1

At the moment I am doing a foreach over the array, and creating a new array as I go. For example:

    $newMap = [];
    foreach ($oldMap as $key => $value) {

        // Apply some complex logic to $value to extract a string
        $newString = my_value_processor($value);

        $newkey = $key + $newString;
        $newMap[$newKey] = $value;
    }

I have the feeling I should be able to accomplish this with array_map(), or possibly array_walk(), and that it would be more efficient, but I can't seem to make it work.

EDIT: In the example above, the code was simplified to show that the $newKey is dependent on $value. The actuality, $value is a sub-array, and I apply some complex logic to it to extract a string. I have updated the example above to demonstrate that.

DatsunBing
  • 8,684
  • 17
  • 87
  • 172
  • IIRC, `foreach` is a lot faster than the `array_` functions. Let me find a source to back that up though. **EDIT** found my source: https://github.com/lizards-and-pumpkins/catalog/issues/528 . It depends on the exact operation, but foreach is always faster and probably even easier to read if you haven't got a background in functional programming. – Loek Jun 07 '18 at 10:28
  • Possible duplicate of [php replace all occurances of key from array in string](https://stackoverflow.com/questions/20126344/php-replace-all-occurances-of-key-from-array-in-string) – Dinesh Ghule Jun 07 '18 at 10:29
  • 1
    No, you can't mutate keys in existing Php arrays. – Progrock Jun 07 '18 at 10:29
  • `foreach ($old as $k => $v) $new[$k.'_'.$v] = $v;` as an example isn't that arduous is it? – Progrock Jun 07 '18 at 10:34
  • Strings can't be merge using `+` operator, use `.` for concatenation – Ankit Singh Jun 07 '18 at 10:39
  • Possible duplicate of [In PHP, how do you change the key of an array element?](https://stackoverflow.com/questions/240660/in-php-how-do-you-change-the-key-of-an-array-element) – Nigel Ren Jun 07 '18 at 10:45

3 Answers3

0

You may use array_map if you want to modify array values (pay attention that here anonymous function used, that is available from PHP 7)

Modifying array values with array_map

$array = array(
  "k1" => "1",  
  "k2" => "2", 
  "k3" => "3",
);

$mapFunc = function($item) {
  return $item . '_';
};

$newArray = array_map($mapFunc, $array);

var_dump($newArray);

Result:

array(3) {
  ["k1"]=>
  string(2) "1_"
  ["k2"]=>
  string(2) "2_"
  ["k3"]=>
  string(2) "3_"
}

You may use array_reduce if you want to modify array keys or values or both

Modifying array keys with array_reduce

$array = array(
  "k1" => "1",  
  "k2" => "2", 
  "k3" => "3",
);

$reduceFunc = function($carry, $key)  use ($array) {
  $carry[$key . '_'] = $array[$key];
  return $carry;
};

$newArray = array_reduce(
  $array,
  $reduceFunc,
  []
);

var_dump($newArray);

Result:

array(3) {
  ["1_"]=>
  string(1) "1"
  ["2_"]=>
  string(1) "2"
  ["3_"]=>
  string(1) "3"
}

But from my point of view it is functional style of programming, that is not typical for php, so i think your foreach approach is better.

Sasha Kos
  • 2,480
  • 2
  • 22
  • 37
-1

Try this code, this code with array_walk().

  $newMap = [];
  array_walk($oldMap, function ($value,$key) use (&$newMap) { 
    $newkey = $key + $value + 'blah blah blah';  
    $newMap[$newkey] = $value;
  });

If are you use string as 'blah blah blah' then replace "+"(plus) to"."(dot).

Mukesh Singh Thakur
  • 1,335
  • 2
  • 10
  • 23
-1

You can try this way,

$oldMap = ['test12' => 'test'];
foreach($oldMap as $key => $value)
    {
        $oldMap[$key.'_'.$value.'blah blah'] = $value;
        unset($oldMap[$key]);
    }
var_dump($oldMap);
Mayank Majithia
  • 1,916
  • 16
  • 21