PHP组合两个多维数组 [英] PHP combine two multi-dimensional arrays

查看:134
本文介绍了PHP组合两个多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用array_combine两个多维阵列组合,但不知何故没有做正确。

下面是数组1:

 阵列(
    [2011年11月18日] =>阵列(
        [C] => 107705.5792
        [Ⅰ] => 44561.52
    )
    [2011年11月22日] =>阵列(
        [C] => -8992.8352
    )

和这里是数组2:

 阵列(
    [2011年11月18日] =>阵列(
        [C] => 3
        [Ⅰ] => 1
    )
    [2011年11月22日] =>阵列(
        [C] => 2
    )

下面是我array_combine的尝试,这是行不通的:

  $ ARRAY1 =($ ARR1);
$数组2 =($ ARR2);
$结果= array_combine($ ARR1,$ ARR2);
呼应'< pre>';
的print_r($结果);
呼应'< / pre>';

我是什么做错了吗?这就是我要寻找的结果:

 阵列(
    [2011年11月18日] =>阵列(
        [3] => 107705.5792
        [1] => 44561.52
    )
    [2011年11月22日] =>阵列(
        [2] => -8992.8352
    )

感谢您的帮助。


  • 编辑 -

我发现,如果我改用array_merge_recursive,这是我的,我得到的结果。不是我一直在寻找,但接近:

 阵列(
    [2011年11月18日] =>阵列(
        [C] =>阵列(
            [0] => 3
            [1] => 107705.5792
        )
        [Ⅰ] =>阵列(
            [0] => 1
            [1] => 44561.52
        )
    )
    [2011年11月22日] =>阵列(
        [C] =>阵列(
            [0] => 2
            [1] => -8992.8352
        )
    )


  • 进一步编辑 -

下面是我曾试图实施以下建议之一,然而这不是为我工作的方式。什么是错误的:

 函数cust_array_merge(阵列和放大器; $数组1,数组$数组2){
    //通过主阵列循环
    的foreach($数组1为$关键=> $ VAL){
        //检查$数组2具有相同的索引
        如果(array_key_exists($键,$数组2)){
            // $数组1的指数恢复到$数组2的值
            的foreach($数组2 [$关键]为$子项= GT; $ subVal){
                如果(array_key_exists($子项,$ ARRAY1 [$关键])){
                    $ tempVal = $ ARRAY1 [$关键] [$子项];
                    未设置($数组1 [$关键] [$子键]);
                    $ ARRAY1 [$键] [$ subVal] = $ tempVal;}}}}}$合并= cust_array_merge($ arr_cats_per_bill_date,$ arr_cvat);
呼应'< pre>';
    的print_r($合并);
呼应'< / pre>';


解决方案

array_merge_recursive 让你非常接近(你的钥匙是叶数组索引1,您的值在索引0)。所以,做它在两个步骤:首先获得合并数组,然后用树枝拨弄得到它的权利。

  //这是array_map回调(),它应该有一些更通用的用途。
//阵列(阵列('K','V'),...) - GT;阵列('K'=>'V',...)
功能flatten_branches($分支机构){
    $ newleaves =阵列();
    的foreach($分支机构$叶){
        $ newleaves [$叶[0]] = $叶[1];
    }
    返回$ newleaves;
}功能merge_flatten_branches($ karray,$ VARRAY){
    // $ karray有钥匙,叶,$ VARRAY具有价值的叶子
    $ M1 = array_merge_recursive($ karray,$ VARRAY);
    返回array_map('flatten_branches',$ M1);
}$合并= merge_flatten_branches($数组2,$数组1);
的print_r($合并);

只是为了好玩,这里有两个方法。这两者都是比 merge_flatten_branches 慢了一点,但说明了一些有用的阵列概念。 (在比PHP等更多的功能味的语言,这可能是preferred。)

 函数merge_flatten_branches_reduce($ karray,$ VARRAY){
    // $ karray有钥匙,叶,$ VARRAY具有价值的叶子
    $ M1 = array_merge_recursive($ karray,$ VARRAY);
    返回array_map('flatten_branches_reduce',$ M1);
}功能merge_flatten_branches_add($ karray,$ VARRAY){
    // $ karray有钥匙,叶,$ VARRAY具有价值的叶子
    $ M1 = array_merge_recursive($ karray,$ VARRAY);
    返回array_map('flatten_branches_add',$ M1);
}//下面的功能是为上述两个回调。功能array_add($ A1,A2 $){
    返回$ A1 + A2 $;
}功能flatten_leaf($叶){
    返回阵列($叶[0] => $叶[1]);
}功能flatten_branches_add($分支机构){
    $叶= array_map('flatten_leaf',($分支机构));
    $ finalleaves =阵列();
    的foreach($叶为$叶){
        $ finalleaves + = $叶;
    }
    返回$ finalleaves;
}
功能flatten_branches_reduce($分支机构){
    $ L = array_map('flatten_leaf',($分支机构));
    返回array_reduce($升,'array_add',阵列());
}

I am trying to use array_combine to combine two multi-dimensional arrays, but somehow not doing it correctly.

Here is array1:

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

and here is array2:

Array(
    [Nov 18, 2011] => Array(
        [C] => 3
        [I] => 1
    )
    [Nov 22, 2011] => Array(
        [C] => 2
    )
)

Here is my attempt at array_combine, which is not working:

$array1 = ($arr1);
$array2 = ($arr2);
$result = array_combine($arr1, $arr2);
echo '<pre>';
print_r($result);
echo '</pre>';

What am I doing wrong? This is the result that I am looking for:

Array(
    [Nov 18, 2011] => Array(
        [3] => 107705.5792
        [1] => 44561.52
    )
    [Nov 22, 2011] => Array(
        [2] => -8992.8352
    )
)

Thanks for your help.

  • EDIT -

I have found that if I instead use array_merge_recursive, this is my the result that I get. Not what I was looking for, but close:

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
        )
    )
)

  • FURTHER EDIT -

Here is the way that I have tried to implement one of the suggestions below, however this is not working for me. What is wrong?:

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 gets you very close (your "key" is in index 1 of leaf arrays, and your value is in index 0). So do it in two steps: first get the merged array, then fiddle with the branches to get it right.

// 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);

Just for fun, here are two more approaches. Both of these are a bit slower than merge_flatten_branches, but illustrate some useful array concepts. (In other more functional-flavored languages than php, these might be preferred.)

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组合两个多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆