1

I have 2 arrays of different length:

$array1 = Array
(
    [0] => Array
        (
            ['_id'] => "Group1"
            ['M'] => 0
            ['F'] => 0
        )
    [1] => Array
        (
            ['_id'] => "Group2"
            ['M'] => 0
            ['F'] => 0
        )
    [2] => Array
        (
            ['_id'] => "Group3"
            ['M'] => 0
            ['F'] => 0
        )
    [3] => Array
        (
            ['_id'] => "Group4"
            ['M'] => 0
            ['F'] => 0
        )
)


$array2 = Array
(
    [0] => Array
        (
            ['_id'] => "Group2"
            ['M'] => 180
            ['F'] => 200
        )
    [1] => Array
        (
            ['_id'] => "Group4"
            ['M'] => 360
            ['F'] => 500
        )
)

I want to compare the values of ['_id'] in both array and if they match, I will replace the values of ['M'] and ['F'] in array1 with those from array2 based on the corresponding ['_id].

So my desired ouptput would be:

$array1 = Array
(
    [0] => Array
        (
            ['_id'] => "Group1"
            ['M'] => 0
            ['F'] => 0
        )
    [1] => Array
        (
            ['_id'] => "Group2"
            ['M'] => 180
            ['F'] => 200
        )
    [2] => Array
        (
            ['_id'] => "Group3"
            ['M'] => 360
            ['F'] => 500
        )
    [3] => Array
        (
            ['_id'] => "Group4"
            ['M'] => 0
            ['F'] => 0
        )
)

This is my code but I can't seem to get the values replaced with the new values. The values are still the same as before.

foreach ($array1 as $defArr)
{
  foreach ($array2 as $dayArr)
  {
    if($dayArr['_id'] == $defArr['_id'])
    {
      $defArr['M'] = $dayArr['M'];
      $defArr['F'] = $dayArr['F'];
    }
  }
}
seankoh
  • 35
  • 1
  • 10
  • Possible duplicate of [PHP foreach change original array values](https://stackoverflow.com/questions/15024616/php-foreach-change-original-array-values) – ggorlen Oct 01 '18 at 03:38

2 Answers2

3

This can be a one-character change:

foreach ($array1 as $defArr)

goes to

foreach ($array1 as &$defArr)
#                   ^

The & reference operator points to the original sub array in the foreach loop context rather than a temporary variable.

However, it's a bit safer to use the index explicitly:

foreach ($array1 as $i => $defArr) {
    foreach ($array2 as $j => $dayArr) {
        if ($dayArr['_id'] == $defArr['_id']) {
            $array1[$i]['M'] = $array2[$j]['M'];
            $array1[$i]['F'] = $array2[$j]['F'];
        }
    }
}

If speed is important or $array2 is large, the time complexity of your algorithm is O(n * m). I recommend hashing $array2 for fast lookups as follows (O(n)):

$lookup = array_reduce($array2, function ($a, $e) {
    $a[$e['_id']] = $e;
    return $a;
});

foreach ($array1 as $i => $e) {
    if (array_key_exists($e['_id'], $lookup)) {
        $array1[$i]['M'] = $lookup[$e['_id']]['M'];
        $array1[$i]['F'] = $lookup[$e['_id']]['F'];
    }
}

Try it!

ggorlen
  • 44,755
  • 7
  • 76
  • 106
0
<?php
$array1 = [['_id'=>'abc','M'=>0,'F'=>0],['_id'=>'abcd','M'=>0,'F'=>0],['_id'=>'abcde','M'=>0,'F'=>0]];

$array2 = [['_id'=>'abc','M'=>50,'F'=>300],['_id'=>'abcde','M'=>600,'F'=>700]];

foreach($array2 as $key=> $array2value){
  $searched_in_array_1= array_search($array2value['_id'],array_column($array1,'_id'));
  if(is_numeric($searched_in_array_1)) $array1[$searched_in_array_1] = $array2value;
}

var_dump($array1);
?>

You want to get a help with php array functions when dealing with arrays. I have used array_column and array_search functions for this