我正在尝试使用
array_combine()
来组合两个多维数组,但不知何故没有正确执行。
这是数组1:
Array(
[Nov 18, 2011] => Array(
[C] => 107705.5792
[I] => 44561.52
)
[Nov 22, 2011] => Array(
[C] => -8992.8352
)
)
这是数组2:
Array(
[Nov 18, 2011] => Array(
[C] => 3
[I] => 1
)
[Nov 22, 2011] => Array(
[C] => 2
)
)
这是我对 array_combine 的尝试,但不起作用:
$array1 = ($arr1);
$array2 = ($arr2);
$result = array_combine($arr1, $arr2);
echo '<pre>';
print_r($result);
echo '</pre>';
我做错了什么? 这就是我正在寻找的结果:
Array(
[Nov 18, 2011] => Array(
[3] => 107705.5792
[1] => 44561.52
)
[Nov 22, 2011] => Array(
[2] => -8992.8352
)
)
我发现如果我改用
array_merge_recursive()
,这就是我得到的结果。 不是我想要的,但很接近:
Array(
[Nov 18, 2011] => Array(
[C] => Array(
[0] => 3
[1] => 107705.5792
)
[I] => Array(
[0] => 1
[1] => 44561.52
)
)
[Nov 22, 2011] => Array(
[C] => Array(
[0] => 2
[1] => -8992.8352
)
)
)
这是我尝试实施以下建议之一的方法,但这对我不起作用。 怎么了?:
function cust_array_merge(array &$array1, array $array2) {
// loop through main array
foreach ($array1 as $key => $val) {
// check if $array2 has the same index
if (array_key_exists($key, $array2)) {
// reset $array1's indexes to $array2's values
foreach ($array2[$key] as $subKey => $subVal) {
if (array_key_exists($subKey, $array1[$key])) {
$tempVal = $array1[$key][$subKey];
unset($array1[$key][$subKey]);
$array1[$key][$subVal] = $tempVal;
}
}
}
}
}
$merged = cust_array_merge($arr_cats_per_bill_date, $arr_cvat);
echo '<pre>';
print_r($merged);
echo '</pre>';
array_merge_recursive
让你非常接近(你的“键”位于叶数组的索引 1 中,你的值位于索引 0 中)。因此,分两步进行:首先获取合并的数组,然后摆弄分支以使其正确。
// This is a callback for array_map() which should have some more generic uses.
// array(array('k', 'v'), ...) -> array('k' => 'v', ...)
function flatten_branches($branches) {
$newleaves = array();
foreach ($branches as $leaf) {
$newleaves[$leaf[0]] = $leaf[1];
}
return $newleaves;
}
function merge_flatten_branches($karray, $varray) {
//$karray has the key-leaves, and $varray has the value-leaves
$m1 = array_merge_recursive($karray, $varray);
return array_map('flatten_branches', $m1);
}
$merged = merge_flatten_branches($array2, $array1);
print_r($merged);
只是为了好玩,这里还有两种方法。这两个都比
merge_flatten_branches
慢一点,但说明了一些有用的数组概念。 (在其他比 php 更函数式的语言中,这些可能是首选。)
function merge_flatten_branches_reduce($karray, $varray) {
//$karray has the key-leaves, and $varray has the value-leaves
$m1 = array_merge_recursive($karray, $varray);
return array_map('flatten_branches_reduce', $m1);
}
function merge_flatten_branches_add($karray, $varray) {
//$karray has the key-leaves, and $varray has the value-leaves
$m1 = array_merge_recursive($karray, $varray);
return array_map('flatten_branches_add', $m1);
}
// The functions below are callbacks for the two above.
function array_add($a1, $a2) {
return $a1+$a2;
}
function flatten_leaf($leaf) {
return array($leaf[0] => $leaf[1]);
}
function flatten_branches_add($branches) {
$leaves = array_map('flatten_leaf', ($branches));
$finalleaves = array();
foreach ($leaves as $leaf) {
$finalleaves += $leaf;
}
return $finalleaves;
}
function flatten_branches_reduce($branches) {
$l = array_map('flatten_leaf', ($branches));
return array_reduce($l, 'array_add', array());
}
您正在寻找的结果对我来说看起来非常定制。也就是说,我不知道有任何内置 PHP 数组函数可以做到这一点。 不过,我确实为你编写了一个自定义函数。 警告:它是非常适合这种场合的,因此可能不太可重复使用。
function cust_array_merge(array $array1, array $array2)
{
$merged = array();
// loop through main array
foreach ($array1 as $key => $val) {
// check if $array2 has the same index
if (array_key_exists($key, $array2)) {
// reset $array1's indexes to $array2's values
foreach ($array2[$key] as $subKey => $subVal) {
if (array_key_exists($subKey, $array1[$key])) {
$tempVal = $array1[$key][$subKey];
unset($array1[$key][$subKey]);
$array1[$key][$subVal] = $tempVal;
}
}
$merged = $array1;
}
}
return $merged;
}
$result = [];
foreach ($array1 as $date => $row) {
foreach ($row as $letter => $total) {
$span = $array2[$date][$letter];
$result[$date][$span] = $total;
}
}
var_export($result);
输出:
array (
'Nov 18, 2011' =>
array (
3 => 107705.5792,
1 => 44561.52,
),
'Nov 22, 2011' =>
array (
2 => -8992.8352,
),
)
不过我想警告你。将值合并到键值对中以减少数组的总体大小是很诱人的,但如果发生变化,跨度可能会在任何给定日期重复,那么您将丢失数据。 只要注意风险即可。 在另一个页面上遵循 @hakre 的建议会更稳定,在另一个页面上创建带有跨度和总计的两个元素行。 您的
array_merge_recursive()
电话可能是最理想的。