The structure is as follows:

arr [ inner1[], inner2[], ... ] 

I loop through the arr array:

 foreach ($arr as $inner) { $inner[...] = ...; } 

In general, I produce actions with a nested array, but I understand that the $inner value in foreach not a pointer to the nested array, so no changes are made to the array itself.

The only option I can see is passing through the array with a normal loop and accessing each element by index. Is this really the only option?

  • the $ inner variable with foreach already contains the value of inner1 and inner2, what you can't get? - Arsen

2 answers 2

 foreach ($arr as &$inner) { $inner[...] = ...; } 
  • 3
    I will add. After this loop, it is advisable to unset ($ inner); - Vorobyov Alexander

You can use array_map (callback, array) to apply the calback function to all elements of an array.

 function mycallback($v) { $v=strtoupper($v); return $v; } $a=array("Animal" => "horse", "Type" => "mammal"); array_map("mycallback", $a) 

will return

 Array ( [Animal] => HORSE [Type] => MAMMAL ) 

Here is an example of using https://www.w3schools.com/php/func_array_map.asp

If, for some criteria, you need to delete part of an array, you can use array_filter (array, callback)

 function test_odd($var) { return($var & 1); } $a1=array("a","b",2,3,4); array_map($a1, "test_odd") 

will return

 Array ( [3] => 3 ) 

https://www.w3schools.com/php/func_array_filter.asp