本文介绍了如何基于PHP中的键合并或组合2个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个数组,我想合并或组合它们...

i have 2 array and i want to merge or combine them...

Array
(
    [0] => Array
        (
            [year] => 2015
            [value] => 32
        )

    [1] => Array
        (
            [year] => 2016
            [value] => 54
        )
)

Array
(
    [0] => Array
        (
            [year] => 2015
            [value] => 95
        )

    [1] => Array
        (
            [year] => 2016
            [value] => 2068
        )

)

我希望他们看起来像这样...

i want them to look like this...

Array(
    [2015]=>array(
        [0] => 32
        [1] => 95
    )
    [2016]=>array(
        [0] => 54
        [1] => 2068
    )
)

这可能吗?如果有的话,如何?....非常感谢

it this possible? if ever, how?.... thanks so much

推荐答案

 $a = array(
     0 => array
         (
            "year" => 2015,
            "value" => 32
         ),
     1 => array
         (
            "year" => 2016,
            "value" => 54
         )
 );

 $b = array(
     0 => array
        (
           "year" => 2015,
           "value" => 300
        ),
    1 => array
       (
           "year" => 2016,
           "value" => 5400
       )
);

$c = array_merge($a,$b);

$output = array();
foreach($c as $key=>$val)
{
    $output[$val['year']][] = $val['value'];
}

echo '<pre>';
print_r($output);
exit;

尝试此代码.

这篇关于如何基于PHP中的键合并或组合2个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 15:40