0

I know the problem can be solved using foreach, but I wonder if there is another way of doing this in fewer lines.

ex:

$arr1 = [
  ['example1'=>'ex1','example2'=>'ex2','example3'=>'ex3'],
  ['example4'=>'ex4','example5'=>'ex5','example6'=>'ex6'],
  ['example7'=>'ex7','example8'=>'ex8','example9'=>'ex9'],
]:

$arr2 = ['example'=>'examplelist'];

wanted results:

$result =  [
  ['example1'=>'ex1','example2'=>'ex2','example3'=>'ex3','example'=>'examplelist'],
  ['example4'=>'ex4','example5'=>'ex5','example6'=>'ex6','example'=>'examplelist'],
  ['example7'=>'ex7','example8'=>'ex8','example9'=>'ex9','example'=>'examplelist'],
]:
6
  • You want to append to every element of the array without iterating over it? Commented Mar 26, 2020 at 18:26
  • @AnuragSrivastava I believe he's asking for a way with built in functions. Commented Mar 26, 2020 at 18:27
  • @El_Vanja You mean loops? Commented Mar 26, 2020 at 18:27
  • @AnuragSrivastava I mean without explicitly defining any loops. Stuff like array_map or array_walk. Commented Mar 26, 2020 at 18:28
  • 1
    You could go with array_walk() but this will not save any lines. Also I do not think it is a reasonable goal to do anything with "fewer lines". Commented Mar 26, 2020 at 18:31

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.