I have a list of codes that need to replace values in an array. This process should leave the other elements in the array unchanged. For example, I have an array that looks like this:
$data=array(
'container_label_1' => '1 gallon',
'container_num_1' => '1',
'container_label_2' => '5 gallon',
'container_num_2' => '1',
'container_label_3' => '2',
'container_num_3' => '5 gallon' );
And I have a second array of variable length that looks like this
$modifier=array(
'1 gallon'=>'1 gallon code',
'5 gallon'=>'5 gallon code',
'10 gallon'=>'10 gallon code'
in the format of:
label value to be replaced=>code
(In actual use the code values I'm using here would be something else that didn't include the container size.)
I want the array to look like this when it's done:
$data=array(
'container_label_1' => '1 gallon code',
'container_num_1' => '1',
'container_label_2' => '5 gallon code',
'container_num_2' => '1',
'container_label_3' => '2',
'container_num_3' => '5 gallon code');
It should only modify container labels (container_label_1, container_label_2,container_label_3, etc). The items in the $modifier array will not necessarily be in the $data array as shown in the example.
It seems like there should be a fairly simple way to accomplish this, but I'm just not thinking of it. I've tried looking for similar cases on here and in the php.net documentation and I was thinking about using array_map, but I just can't seem to wrap my head around how this would work with my situation. I'm looking for something that's more efficient than checking every array item for each item in the modifier array as these arrays are much larger than the example.
I saw something that looked promising here: http://www.php.net/manual/en/function.array-replace.php steelpandrummer's post seems to do something close to what I want, but it compares keys and I need to compare values, not keys. I can't do an array flip because my values are not often going to be unique. And array flip would thus lose data.
Any help would be appreciated.