PHP: How to match two multidimensional array

前端 未结 3 529
没有蜡笔的小新
没有蜡笔的小新 2021-01-26 02:41

I stuck on this and really don\'t know how to solve it. I have two multi-dimensional arrays and need to match every \"entry_id\" from second array with first one. Then need to c

3条回答
  •  迷失自我
    2021-01-26 03:09

    I found a solution for you :

    $a1 = [['entry_id' => 1, 'file_no' => 'KSBR', 'status' => 'abc'], ['entry_id' => 2, 'file_no' => 'KSUL', 'status' => 'defg']];
    $a2 = [['entry_id' => 1, 'file_no' => 'KSBR', 'status' => 'abc', 'type' => 'person'], ['entry_id' => 2, 'file_no' => 'KSUL', 'status' => 'defg']];
    
    print_r(new_array_merge_recursive($a1, $a2));
    
    function new_array_merge_recursive(array $array1, array $array2=array())
    {
        $arrays = func_get_args();
    
        $merge = array_shift($arrays);
    
        foreach ($arrays as $array)
        {
            foreach ($array as $key => $val)
            {
                if (is_array($val) && array_key_exists($key, $merge))
                {
                    $val = new_array_merge_recursive((array) $merge[$key], $val);                                                                                                                               
                }
    
                $merge[$key] = $val;
            }
        }
    
        return $merge;
    }
    

提交回复
热议问题