Hello!

There is a multidimensional array of $allCategories with unknown nesting, you need to make several one-dimensional arrays of 1 level from it, for this I wrote the function convertToSimpleArray .

To combine the result into a single array, use the line global $resArray; i.e. each time the function is called from the loop, the $resArray must be cleared, so, what is now written in the foreach loop does not work as it should, the $resArray not cleared, how to fix it?

 function convertToSimpleArray($array){ global $resArray; if(is_array($array)){ foreach($array as $below){ $res = convertToSimpleArray($below); } } else { $resArray[] = $array; } return $resArray; } $simpleArraysCategories = []; $count = 0; foreach($allCategories as $category){ $resArray = []; $simpleArraysCategories[$count] = convertToSimpleArray($allCategories[$count]); $count++; } 

PS I'm new to php, come from js, at least there are closures there.

  • why do you need a global array if at the global level it is not used at all? Why cycle the $category if you use $count - teran
  • one
    you would have led the format of the source array better, and the look of the final arrays would have suggested a more convenient way. - teran
  • and pay attention to the array_walk_recursive function, maybe you will not need to fence a lot of code? - teran
  • @teran which is remarkable ru.stackoverflow.com/a/711187/191482 :) - Alexey Shimansky
  • @ AlekseyShimansky yes indeed :) - teran

0