PHP:如何匹配两个多维数组

时间:2014-04-08 11:31:22

标签: php arrays

我坚持这一点,并且真的不知道如何解决它。 我有两个多维数组,需要匹配每个" entry_id"从第二个数组到第一个。然后需要检查每个" file_no"来自第二个数组是在数据库(第一个数组)和" status"与第一个数组匹配。如果" 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
                )    
        )
)

感谢每一条评论并抱歉英语:)

3 个答案:

答案 0 :(得分:0)

这应该有效

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

答案 1 :(得分:0)

我为您找到了解决方案:

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

答案 2 :(得分:0)

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

$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
                )

        )

)

编辑:这也是可能的,在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);