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.

6
  • IIRC, foreach is a lot faster than the array_ functions. Let me find a source to back that up though. EDIT found my source: 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. Commented Jun 7, 2018 at 10:28
  • Possible duplicate of php replace all occurances of key from array in string Commented Jun 7, 2018 at 10:29
  • 1
    No, you can't mutate keys in existing Php arrays. Commented Jun 7, 2018 at 10:29
  • foreach ($old as $k => $v) $new[$k.'_'.$v] = $v; as an example isn't that arduous is it? Commented Jun 7, 2018 at 10:34
  • Strings can't be merge using + operator, use . for concatenation Commented Jun 7, 2018 at 10:39

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

he don't want to change the value.
thanks, a have add array array_reduce, that allows to modify keys
-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).

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.