在php中将同一数组的2个元素合并为一个

时间:2016-06-01 07:47:49

标签: php arrays merge

我有一个数组

Array
(
    [0] => Array
        (
            [hrg_lid] => 214291464161204318
            [pecon] => 0
            [col2pe] => Karam
            [col4pe] => 1
            [col6pe] => 2
            [col8pe] => 264
            [col9pe] => 42
            [col10pe] => 85
            [col11pe] => 2

        )

    [1] => Array
        (
            [hrg_lid] => 707581464079555092
            [pecon] => 1
            [col2pe] => Dummy
            [col4pe] => 
            [col6pe] => 
            [col8pe] => 12
            [col9pe] => 0
            [col10pe] => 0
            [col11pe] => 2
            [col12pe] => 1
            [col13pe] => 1

        )

    [2] => Array
        (
            [hrg_lid] => 707581464079555092
            [col5risk] => 2
            [col6risk] => 2
            [col7risk] => 1
            [col8risk] => 2
            [col9risk] => 1
            [col10risk] => 1
            [col11risk] => 2
        )

我想合并那些具有相同 hrg_lid 的元素。 预期产出

Array
(
    [0] => Array
        (
            [hrg_lid] => 214291464161204318
            [pecon] => 0
            [col2pe] => Karam
            [col4pe] => 1
            [col6pe] => 2
            [col8pe] => 264
            [col9pe] => 42
            [col10pe] => 85
            [col11pe] => 2

        )

    [1] => Array
        (
            [hrg_lid] => 707581464079555092
            [pecon] => 1
            [col2pe] => Dummy
            [col4pe] => 
            [col6pe] => 
            [col8pe] => 12
            [col9pe] => 0
            [col10pe] => 0
            [col11pe] => 2
            [col12pe] => 1
            [col13pe] => 1
            [col5risk] => 2
            [col6risk] => 2
            [col7risk] => 1
            [col8risk] => 2
            [col9risk] => 1
            [col10risk] => 1
            [col11risk] => 2

        )

我尝试了以下代码

foreach($arr as $key => $value) {
       $finalArray[$value['hrg_lid']] = $value;

   }

但失败

4 个答案:

答案 0 :(得分:1)

我会使用hrg_lid作为数组键 - 否则每次添加新元素时都必须检查输出数组中的每个元素是否匹配:

$finalArray = array();

foreach($arr as $value) {
    $hrg_lid = $value['hrg_lid'];
    if (isset($finalArray[$hrg_lid])) {
        // merge if element with this $hrg_lid is already present
        $finalArray[$hrg_lid] = array_merge($finalArray[$hrg_lid], $value);
    } else {
        // save as new
        $finalArray[$hrg_lid] = $value;
    }
}

如果要获得规范化的数组键,可以在以后重置它们:

$finalArray = array_values($finalArray);

答案 1 :(得分:0)

hrg_lid值必须是数组的键,如果您不能更改键,请尝试以下操作:

  [self performSegueWithIdentifier:@"HomeToSettings" sender:self];

答案 2 :(得分:0)

尝试如下:

$finalArray = [];

foreach($arr as $singleArray) {
    $id = $singleArray['hrg_lid'];
    if (isset($finalArray[$id])) {
        $finalArray = array_merge($finalArray[$id], $singleArray);
    } else {
        $finalArray[] = $singleArray;
    }
}

答案 3 :(得分:0)

你可以尝试类似的东西:

$tmpArray = array();
$finalArray = array();

// We merge the arrays which have the same value in 'hrg_lid' col
foreach($source as $array){
    $key = $array['hrg_lid'];
    array_shift($array);
    if(array_key_exists($key, $tmpArray)){
        $tmpArray[$key] = array_merge($tmpArray[$key], $array);
    }else{
        $tmpArray[$key] = $array;
    }
}

// We build the final array
foreach($tmpArray as $key => $value){
    $finalArray[] = array_merge(array('hrg_lid' => $key), $value);
}