1

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.

starshine531
  • 601
  • 5
  • 19

2 Answers2

2

Actually, array_map works ok :

$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'
);


function replaceValues($val) {
    $modifier = array(
        '1 gallon' => '1 gallon code',
        '5 gallon' => '5 gallon code',
        '10 gallon' => '10 gallon code'
    );
    return isset($modifier[$val]) ? $modifier[$val] : $val;
}

print_r(array_map('replaceValues', $data));

Result is

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
)
Katran
  • 86
  • 1
  • 2
  • Thank you much for you swift reply. I haven't had much experience using php array manipulation functions as I usually manage to do this in sql instead, but in this case I just couldn't. I've been staring at this thing for hours and just not getting it. Your assistance is very much appreciated. – starshine531 Oct 03 '13 at 09:07
  • I think I'll need to edit this to pass the modifier array instead of defining it within as the modifier array is variable. However, it gives me the example I needed. I can manage the rest. :) – starshine531 Oct 03 '13 at 09:17
1

Clean version with lambda function:

array_walk($data, function(&$v, $k) {
    global $modifier;
    $v = array_key_exists($v, $modifier) ? $modifier[$v] : $v;
});
Patryk
  • 618
  • 7
  • 17
  • Thanks for the answer. Would this be any more efficient than the array_map example by Katran? The modifier array will not always be the same. – starshine531 Oct 03 '13 at 09:13
  • Much cleaner syntax for function definition, which can be optimised for performance by opcode caches. Apart from that not much of a difference. – Patryk Oct 03 '13 at 09:19
  • Exact differences between these 2 functions are well described here: [link](http://stackoverflow.com/questions/3432257/difference-between-array-map-array-walk-and-array-filter) – Katran Oct 03 '13 at 09:47
  • I'm changing the answer I accepted because I ended up using the array_walk instead of the array_map. Thank you both for the help. – starshine531 Oct 03 '13 at 11:33