Skip to content
Advertisement

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

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 🙂

Advertisement

Answer

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

Output:

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: 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);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement