php合并相同数组的数组

时间:2017-03-15 14:12:20

标签: php arrays array-merge

我在php中有这个数组:

array:5 [
  0 => array:3 [
    "tab" => "9"
    "layout_id" => "11"
    "banners_ids" => "7"
  ]
  1 => array:3 [
    "tab" => "9"
    "layout_id" => "11"
    "banners_ids" => "8"
  ]
  2 => array:3 [
    "tab" => "10"
    "layout_id" => null
    "banners_ids" => null
  ]
  3 => array:3 [
    "tab" => "11"
    "layout_id" => null
    "banners_ids" => null
  ]
  4 => array:3 [
    "tab" => "12"
    "layout_id" => null
    "banners_ids" => null
  ]
]

我需要在同一个数组中合并那些具有相同" tab"有这样的事情的关键:

array:5 [
  0 => array:3 [
    "tab" => "9"
    "layout_id" => "11"
    "banners_ids" => "8,7"
  ]
  1 => array:3 [
    "tab" => "10"
    "layout_id" => null
    "banners_ids" => null
  ]
  2 => array:3 [
    "tab" => "11"
    "layout_id" => null
    "banners_ids" => null
  ]
  3 => array:3 [
    "tab" => "12"
    "layout_id" => null
    "banners_ids" => null
  ]
]

我怎样才能达到这个目标?我和foreach一起玩,并且像current()或prev()那样没有结果。

3 个答案:

答案 0 :(得分:1)

$finala = $c = $arr;
echo "<pre>"; print_r($finala);echo "</pre>";
foreach($arr as $ke=>$ss){
foreach($c as $k => $cc){
    if($ke == $k) continue;
    if($ss['tab'] == $cc['tab']){            
        $finala[$ke]['tab'] = $ss['tab'];
        $finala[$ke]['layout_id'] = $ss['layout_id'];
        $finala[$ke]['banners_ids'] = implode(',', array( $cc['banners_ids'],$ss['banners_ids']));
        unset($finala[$k]);
    }
}
}
ksort($finala);
echo "<pre>"; print_r($finala);echo "</pre>";

答案 1 :(得分:1)

分心了事情。无论是使用Bhaskar的答案还是我的答案......或者将它们结合起来。

<?php

$array=json_decode('[{"tab":"9","layout_id":"11","banners_ids":"7"},{"tab":"9","layout_id":"11","banners_ids":"8"},{"tab":"10","layout_id":null,"banners_ids":null},{"tab":"11","layout_id":null,"banners_ids":null},{"tab":"12","layout_id":null,"banners_ids":null}]',true);


//var_dump($array);
$new_array=array();
foreach($array as $entry)
{
$new_array[$entry['tab']]['tab']=$entry['tab'];
$new_array[$entry['tab']]['layout_id']=$entry['layout_id'];
$new_array[$entry['tab']]['banners_ids'][]=$entry['banners_ids'];
}

foreach($new_array as $key=>$val)
{
$new_array[$key]['banners_ids']=implode(',',$new_array[$key]['banners_ids']);
}

var_dump($new_array);

答案 2 :(得分:0)

// Manual sort by tab value
usort($array, function($a, $b){
    return $a['tab'] - $b['tab'];
});

$serve = -1;
$merge = array();
foreach ($array as $value) {
    $tab_position = array_search($value['tab'], array_column($array,'tab')); // find same tab value position

    if($tab_position > $serve) $serve = $tab_position;

    if($tab_position == $serve){
        $merge[$tab_position]['tab'] = $value['tab'];
        $merge[$tab_position]['layout_id'] = $value['layout_id']; // if you want to merge layout_id as well, do as below
        $merge[$tab_position]['banners_ids'] = !isset($merge[$tab_position]['banners_ids']) ? 
                                                $value['banners_ids'] : 
                                                $merge[$tab_position]['banners_ids'].','.$value['banners_ids'];

        $merge[$tab_position]['banners_ids'] = rtrim($merge[$tab_position]['banners_ids'], ','); // remove extre comma (.)

    }
}

print_r($merge); // output