There is an N-dimensional array of elements with unique keys of non -arrays of elements. It is necessary to convert it to one-dimensional. The question is perhaps elementary, but I don’t see a suitable function from the standard ones: http://www.php.net/manual/ru/ref.array.php

For example, array_merge_recursive() preserves the nested structure of the original array, array_map() and some others require a callback function, it seems to me that this is not a beautiful solution, since anyway, no conversion is needed on the values ​​of the elements.

  • one
    then it means you need to write your recursive function. It is quite simple. Schematic code f (a: array) {r: array = []; foreach x in a {if (x is array) {appendarray (r, f (x)); } else {append (r, x)}} return r; } appendarray(a,b) - adds the array b to the end of a . append(a,b) - adds the value of b to the end of a . (x is array) - all the instructions that check the type of element in php is, you just need to find the name. - KoVadim
  • If it were not for curly braces, I would think that this is some kind of pseudopiton =) I tried ... <pre> function x ($ array) {foreach ($ array as $ key => $ val) {if (is_array ($ val) ) $ output [] = self :: to_form_names ($ val); else $ output [$ key] = $ val; } return $ output; } </ pre> Does nothing but overwrites the array keys =) - pilot114
  • one
    error in the string $output[$key] = x($val) , you insert the array as one element. And you need to add element by element. In my pseudocode, there is append and appendarray. - KoVadim

1 answer 1

Than callback didn't please you?

 $multiarray = ...; $result = array(); array_walk_recursive($multiarray, function($value, $key) use (&$result){ $result = array($key, $value); // тут возвращаете как вам хочется }); 

Another interesting way:

 $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($multiarray)); $result = array(); foreach($iterator as $key => $value) { $result[] = array($key, $value); // тут возвращаете как вам хочется }