Overwrite subarrays in one multidimensional array if different from another multidimensional array
Asked Answered
C

3

0

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

Coppersmith answered 8/4, 2014 at 11:31 Comment(1)
I find this to be an Unclear question with an insufficient minimal reproducible example / problem statement.Bathurst
S
0

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);
Stearoptene answered 8/4, 2014 at 12:50 Comment(1)
if (!isset($tmparr[$arr1_val['entry_id']])) $tmparr[$arr1_val['entry_id']] = array(); is not necessary because PHP does not require parent elements to be instantiated when pushing child elements with square brace syntax.Bathurst
W
0

This should work

    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;
        }
    }
}
Wildman answered 8/4, 2014 at 11:38 Comment(1)
Thanks for reply, but status output is the same. Don't know how to fix it.Coppersmith
J
0

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;
}
Jugoslavia answered 8/4, 2014 at 12:37 Comment(0)
S
0

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);
Stearoptene answered 8/4, 2014 at 12:50 Comment(1)
if (!isset($tmparr[$arr1_val['entry_id']])) $tmparr[$arr1_val['entry_id']] = array(); is not necessary because PHP does not require parent elements to be instantiated when pushing child elements with square brace syntax.Bathurst

© 2022 - 2024 — McMap. All rights reserved.