如果一个多维数组中的子数组与另一个多维阵列不同,则覆盖该子数组


Overwrite subarrays in one multidimensional array if different from another multidimensional array

我一直坚持这个问题,真的不知道如何解决它。我有两个多维数组,需要将第二个数组中的每个"entry_id"与第一个数组进行匹配。然后需要检查第二个数组中的每个"file_no"是否都在数据库中(第一个数组),以及"status"是否与第一个数组匹配。如果"状态"不同,用字符串(例如更新的值)更新第二个数组,如下所示:

...
[status] => Array
                    (
                        [0] => abc
                        [1] => defghijk - "updated value"
                    )    

所以我有了数据库中的第一个数组:

Array
(
    [0] => Array
        (
            [entry_id] => 1
            [file_no] => KSBR 40 INS 3674 / 2014
            [status] => abc
        )
    [1] => Array
        (
            [entry_id] => 9
            [file_no] => KSUL 77 INS 18898 / 2013
            [status] => abc
        )
    [2] => Array
        (
            [entry_id] => 9
            [file_no] => KSUL 77 INS 21218 / 2013
            [status] => defg
        )
)

第二个由脚本生成的数组:

Array
(
    [0] => Array
        (
            [entry_id] => 1
            [id] => 500910/098
            [fullname] => Milan Vrtal
            [type] => person
            [file_no] => Array
                (
                    [0] => KSBR 26 INS 37146 / 2013
                    [1] => KSBR 40 INS 3674 / 2014
                )
            [status] => Array
                (
                    [0] => status1
                    [1] => status2
                )    
        )
    [1] => Array
        (
            [entry_id] => 2
            [id] => 46900217
            [fullname] => ENTEC a.s.
            [type] => company
            [file_no] => Array
                (
                    [0] => KSBR 28 INS 1232 / 2013
                )
            [status] => Array
                (
                    [0] => qwer
                )
        )
    [2] => Array
        (
            [entry_id] => 9
            [fullname] => Blanka Kořínková
            [type] => person
            [file_no] => Array
                (
                    [0] => KSUL 77 INS 18898 / 2013
                    [1] => KSUL 77 INS 21218 / 2013
                )
            [status] => Array
                (
                    [0] => abc
                    [1] => defghijk
                )    
        )
)

感谢您的每一条评论,并对英文表示歉意:)

这是通过创建一个临时数组来搜索。当数组很大时,这将占用相当多的内存,但会导致更快的执行时间。。。

$tmparr = array();
foreach($arr1 as $arr1_val)
{
  //put an new element in $temparr with key 'entry_id' and an array as value
  if (!isset($tmparr[$arr1_val['entry_id']]))
    $tmparr[$arr1_val['entry_id']] = array();
  //add the status to the array
  $tmparr[$arr1_val['entry_id']][] = $arr1_val['status'];
}
/*
$tmparr = Array
(
    [1] => Array
        (
            [0] => abc
        )
    [9] => Array
        (
            [0] => abc
            [1] => defg
        )
)
*/
//arr2_val by reference so that we can change it
foreach($arr2 as &$arr2_val)
{
  //get the current entry_id
  $entry_id = $arr2_val['entry_id'];
  //see if this entry_id was in the first array, and if so...
  if (isset($tmparr[$entry_id]))
  {
    //change the status to both the original status and the status of the first array
    $arr2_val['status'] = array_merge($arr2_val['status'],$tmparr[$entry_id]);
  }
}
print_r($arr2);

输出:

Array
(
    [0] => Array
        (
            [entry_id] => 1
            [id] => 500910/098
            [fullname] => Milan Vrtal
            [type] => person
            [file_no] => Array
                (
                    [0] => KSBR 26 INS 37146 / 2013
                    [1] => KSBR 40 INS 3674 / 2014
                )
            [status] => Array
                (
                    [0] => status1
                    [1] => status2
                    [2] => abc
                )
        )
    [1] => Array
        (
            [entry_id] => 2
            [id] => 46900217
            [fullname] => ENTEC a.s.
            [type] => company
            [file_no] => Array
                (
                    [0] => KSBR 28 INS 1232 / 2013
                )
            [status] => Array
                (
                    [0] => qwer
                )
        )
    [2] => Array
        (
            [entry_id] => 9
            [fullname] => Blanka Kořínková
            [type] => person
            [file_no] => Array
                (
                    [0] => KSUL 77 INS 18898 / 2013
                    [1] => KSUL 77 INS 21218 / 2013
                )
            [status] => Array
                (
                    [0] => abc
                    [1] => defghijk
                    [2] => abc
                    [3] => defg
                )
        )
)

edit:这也是可能的,去掉temp数组,但循环中有一个循环。这将比第一个慢,但会消耗更少的内存:

//arr2_val by reference so that we can change it
foreach($arr2 as &$arr2_val)
{   
  //get the current entry_id
  $entry_id = $arr2_val['entry_id'];
  //search for the correct row in the first array
  foreach($arr1 as $arr1_val)
  {   
    if ($arr1_val['entry_id'] == $arr2_val['entry_id'])
    {   
      $arr2_val['status'][] = $arr1_val['status'];
      //a continue should be added here to make it faster...
    }
  }   
}   
print_r($arr2);

这应该能在中工作

    foreach($array1 as $i)
{
    foreach($array2 as $key=>$j)
    {
        if($j['entry_id'] == $i['entry_id'])
        {
            if($array2[$key]['status'] != $i['status'])
            {
                $j['status'] = array(
                    $i['status'],
                    $j['status'] // the new status
                );
            }
            continue;
        }
    }
}

我为您找到了一个解决方案:

$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;
}