Question
How do you modify a value in a PHP array if you do not know the structure of the array in advance?
Example
In the following array, I have to change ‘fave_color’ to ‘blue’ in the part of the array where ‘system’ == ‘knoppix’ … the problem is I do not know what the structure of the array will be at runtime, so I cannot simply do:
$myarray['settings']['user_prefs']['otherhost']['fave_color'] = 'blue';
This will not work, because the nesting is unknown for the fave_color at runtime.
Moreover, the fave_color key I am interested in is dependent on system array key.
I have to find the one with the value ‘knoppix’ and then change the corresponding ‘fave_color’ value, making sure not to change any other fave_color value in the array.
'settings' => array(
'user_prefs'=>array(
'localhost'=>array(
'fave_color'=>'orange',
'system' =>'unbuntu',
),
'otherhost'=>array(
'fave_color'=>'yellow',
'system' =>'knoppix',
),
),
),
You can use array_walk_recursive and check if key is equal to ‘knoppix’.