So lets say I have a multidimentional array as such
$myArray = [
0 = [
fruit = 'apple',
juice = 'orange',
cars = [bmw = 'blue', audi = 'red', ford = 'yellow']
],
1 = [
fruit = 'strawberry',
juice = 'grape',
cars = [bmw = 'grey', mazda = 'blue', hummer = 'orange']
],
]
and some replacement array values for cars
$replaceCarsArray = [ferrari = 'red', lamborghini = 'blue, masarati = 'pink']
I foreach through the array with $key => &$values (value being passed by reference)
foreach ($myArray as $key => &$values) {
foreach ($values as $key2 => &$value) {
if ($key2 == 'cars'){
$value = $replaceCarsArray;
}
}
}
that works and replaces the entire cars values with the $replaceCarsArray
but what if I want to target one of the items in that cars array and change the color? So this is what I tried:
foreach ($myArray as $key => &$values) {
foreach ($values as $key2 => &$value) {
if ($key2 == 'cars' && $value['bmw'] != 'red'){
$value['bmw'] = 'red';
}
}
}
this however does not seem to work and the bmw color does not get updated to red. How would I be able to change that data?
Note this is example data and I wrote it very quickly for all intents and purposes I do have access to all of the values and I do not have any syntactical errors in my code as might have appeared here.