PHP:如何匹配两个多维数组 [英] PHP: How to match two multidimensional array

查看:194
本文介绍了PHP:如何匹配两个多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持这个,真不知道如何解决它。
我有两个多维数组和需要匹配从第一个第二个阵列每一个entry_id。这时需要检查,如果从第二阵列每一个file_no是数据库(第一阵列)和地位第1个数组匹配。如果身份不同的是,更新的字符串(例如更新后的值),这样第二个数组:

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 check if every "file_no" from second array is in database (first array) and "status" are matched with 1st array . If "status" is different, update second array with string (e.g. updated value) like this:

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

所以我有从数据库第一个数组:

So I have first array from database:

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
        )

)

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

And second array generated from script:

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

感谢每一个评论,而对于英语惋惜:)

Thanks for every comment and sorry for english :)

推荐答案

这是通过创建一个临时数组中搜索。这将使用相当长的一段记忆,当阵列是大,但会导致更快的执行时间...

This is by creating a temporary array to search in. This will use quite some memory when the arrays are big, but will result in faster execution time...

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

        )

)

编辑:这是可能太,whitout临时数组,但与在一个循环的循环。这将是比第一个慢,但是将消耗更少的存储器

edit: This is possible too, whitout the temp array, but with a loop in a loop. This will be slower than the first one, but will consume less memory:

//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);

这篇关于PHP:如何匹配两个多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆