I have two objects arrays.
First array $x:
[0] => stdClass Object
(
[id] => 54
[value] => test54
[something] => testtest54
)
[1] => stdClass Object
(
[id] => 21
[value] => test21
[something] => testtest21
)
...
Second array $y:
[0] => stdClass Object
(
[id] => 21
[value] => test21_new_value
)
[1] => stdClass Object
(
[id] => 54
[value] => test54_new_value
)
...
I want to update my first array $x importing value of the second array's ($y) field which has the same id, I want to have :
[0] => stdClass Object
(
[id] => 54
[value] => test54_new_value
[something] => testtest54
)
[1] => stdClass Object
(
[id] => 21
[value] => test21_new_value
[something] => testtest21
)
...
I can do something like this :
foreach($x as $k => $v) {
foreach($y as $k2 => $v2) {
if ($v->id === $v2->id) $x[$k]->value = $v2->value;
}
}
But I don't like this, because I have to walk on array $y for each $x (if I have 100 items in $x and 100 items in $y, it loops 100*100 times). I think there is a more efficient, elegant or optimized way to do this, but I don't know how to do that (I did not found a precise response to this specific problem, so I ask here).