当前位置:Gxlcms > PHP教程 > php合并数组~按照某一列的值

php合并数组~按照某一列的值

时间:2021-07-01 10:21:17 帮助过:3人阅读

如这样两个数组:

A=[[0] =>[[time] => 2015-11-01,
          [draw] => 900],
   [1] =>[[time] => 2015-11-02,
          [draw] => 1512],
   [2] =>[[time] => 2015-11-05,
          [draw] => 1600]];
          
B=[[0] =>[[time] => 2015-11-01,
          [data] => 90],
   [1] =>[[time] => 2015-11-03,
          [data] => 152],
   [2] =>[[time] => 2015-11-05,
          [data] => 100]];
          

我要把AB数组合并成一个数组,按照[time]相同的合并在一起,达到这样一个效果:

C=[[0] =>[[time] => 2015-11-01,
          [draw] => 900,  [data] => 90],
   [1] =>[[time] => 2015-11-02,
          [draw] => 1512],
   [2] =>[[time] => 2015-11-03,
                          [data] => 152],
   [3] =>[[time] => 2015-11-05,
          [draw] => 1300, [data] => 100]];
          

类似数据库的"full join", , 这个有办法实现吗?

回复内容:

如这样两个数组:

A=[[0] =>[[time] => 2015-11-01,
          [draw] => 900],
   [1] =>[[time] => 2015-11-02,
          [draw] => 1512],
   [2] =>[[time] => 2015-11-05,
          [draw] => 1600]];
          
B=[[0] =>[[time] => 2015-11-01,
          [data] => 90],
   [1] =>[[time] => 2015-11-03,
          [data] => 152],
   [2] =>[[time] => 2015-11-05,
          [data] => 100]];
          

我要把AB数组合并成一个数组,按照[time]相同的合并在一起,达到这样一个效果:

C=[[0] =>[[time] => 2015-11-01,
          [draw] => 900,  [data] => 90],
   [1] =>[[time] => 2015-11-02,
          [draw] => 1512],
   [2] =>[[time] => 2015-11-03,
                          [data] => 152],
   [3] =>[[time] => 2015-11-05,
          [draw] => 1300, [data] => 100]];
          

类似数据库的"full join", , 这个有办法实现吗?

function setKeyByTime($arr) {
    $res = array();
    foreach($arr as $item) {
        $res[$item['time']] = $item;
    }
    return $res;
}

$C = array_merge_recursive( setKeyByTime($A), setKeyByTime($B) );

人气教程排行