PHP结合了两个多维数组

时间:2011-12-05 05:44:27

标签: php arrays multidimensional-array

我正在尝试使用array_combine来组合两个多维数组,但不知何故不能正确执行。

这是array1:

Array(
    [Nov 18, 2011] => Array(
        [C] => 107705.5792
        [I] => 44561.52
    )
    [Nov 22, 2011] => Array(
        [C] => -8992.8352
    )
)

这里是array2:

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>';

2 个答案:

答案 0 :(得分:3)

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慢一点,但说明了一些有用的数组概念。 (在其他功能更强大的语言中,这些语言可能更受欢迎。)

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());
}

答案 1 :(得分:1)

您正在寻找的结果看起来非常适合我。也就是说,我不知道任何内置的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;
}