通过共享密钥合并多维数组值

时间:2018-05-23 15:42:17

标签: php arrays multidimensional-array

在PHP中使用单个数组,我想像喜欢一样合并,但我能够这样做,但我只得到1返回应该有2个。

下面是原始数组,以及新数组。

    id LIKE 'fghij' OR id LIKE 'abcde' 

   $orig = Array
    (
        [0] => Array
            (
                [id] => abcde
                [field2] => height
                [value2] => 40 ft.
            )

        [1] => Array
            (
                [id] => abcde
                [field3] => width
                [value3] => 10-15 ft.
            )

        [2] => Array
            (
                [id] => abcde
                [field4] => light
                [value4] => Full - Partial
            )

        [3] => Array
            (
                [id] => abcde
                [field6] => space
                [value6] => 5-6 ft.
            )

        [4] => Array
            (
                [id] => fghij
                [field2] => height
                [value2] => 4-8 ft.
            )

        [5] => Array
            (
                [id] => fghij
                [field3] => width
                [value3] => 3-4 ft.
            )

        [6] => Array
            (
                [id] => fghij
                [field4] => light
                [value4] => Full - Partial
            )

        [7] => Array
            (
                [id] => fghij
                [field5] => season
                [value5] => Spring
            )

        [8] => Array
            (
                [id] => fghij
                [field6] => space
                [value6] => 4 ft.
            )

        [9] => Array
            (
                [id] => fghij
                [field19] => restricted
                [value19] => CA, TX, LA, FL, AZ
            )

        [10] => Array
            (
                [id] => abcde
                [field19] => restricted
                [value19] => AZ
            )

    )
   $new = Array
    (
        [id] => abcde
        [field2] => height
        [value2] => 4-8 ft.
        [field3] => width
        [value3] => 3-4 ft.
        [field4] => light
        [value4] => Full - Partial
        [field6] => space
        [value6] => 4 ft.
        [field5] => season
        [value5] => Spring
        [field19] => restricted
        [value19] => AZ
    )

$new非常接近正确,但它忽略了与fghij相关的所有内容,返回了一个扁平数组。我希望用合并的值返回两个id。

这就是我在代码方面的地方:

$new_new = [];
foreach($orig as $key => $value){
  if(is_array($value)){
    $new_new = array_merge($new_new, $value);
  }
}

2 个答案:

答案 0 :(得分:1)

可能这样对你有用吗?您可以为$ value ['id']添加额外的支票。

$new = [];
foreach($arr as $key => $value){
  if(is_array($value)){
    if( !array_key_exists($value['id'],$new) ){
        $new[$value['id']] = [];
    }  
    $new[$value['id']] = array_merge($new[$value['id']], $value );
  }
} 

我改变了我的算法,现在它正如我预期的那样工作。

答案 1 :(得分:1)

您需要累积每个唯一ID的条目。

function mergeByKey($array,$key){
    $result = [];
    foreach($array as $entry){
        $is_new = true;
        foreach($result as &$result_entry){
            if($result_entry[$key]==$entry[$key]){
                $is_new = false;
                $result_entry = array_merge($result_entry,$entry);
                break;
            }
        }
        if($is_new)
            $result[] = $entry;
    }
    return $result;
}

$new_new = mergeByKey($orig,'id');