I have a two-dimensional array of this format:
$oldArr = [
0 => [
"color" => "red",
"shape" => "circle",
"size" => "small",
],
1 => [
"color" => "green",
"shape" => "square",
"size" => "large",
],
2 => [
"color" => "yellow",
"shape" => "triangle",
"size" => "large",
],
];
And a one-dimensional array of this format:
$newVals = [
0 => "large",
1 => "large",
2 => "small",
];
I'm attempting to use str_replace() to iterate through each "size" value in $oldArr, and replace it with the value in $newVals that matches its position. Since these arrays will always have the same number of top-level key-value pairs, I'm basically trying to take $newVals and map it onto each $oldArr["size"] value. The end result should be
$newArr = [
0 => [
"color" => "red",
"shape" => "circle",
"size" => "large",
],
1 => [
"color" => "green",
"shape" => "square",
"size" => "large",
],
2 => [
"color" => "yellow",
"shape" => "triangle",
"size" => "small",
],
];
Can anyone recommend the best way to go about this? I've attempted a str_replace in a foreach loop, but it hasn't worked:
foreach($oldArr as $entity):
str_replace($entity['size'], $newVals, $entity);
endforeach;