I want to generate a list of the second level of keys used. Each record does not contain all of the same keys. But I need to know what all of the keys are. array_keys() doesn't work, it only returns a list of numbers.
Essentially the output Im looking for is:
action, id, validate, Base, Ebase, Ftype, Qty, Type, Label, Unit
I have a large multi-dimensional array that follows the format:
Array
(
[0] => Array
(
[action] => A
[id] => 1
[validate] => yes
[Base] => Array
(
[id] => 2945
)
[EBase] => Array
(
[id] => 398
)
[Qty] => 1
[Type] => Array
(
[id] => 12027
)
[Label] => asfhjaflksdkfhalsdfasdfasdf
[Unit] => asdfas
)
[1] => Array
(
[action] => A
[id] => 2
[validate] => yes
[Base] => Array
(
[id] => 1986
)
[FType] => Array
(
[id] => 6
)
[Qty] => 1
[Type] => Array
(
[id] => 13835
)
[Label] => asdssdasasdf
[Unit] => asdger
)
)
Thanks for the help!
edit: removed superfluous array_reverse() function
try this function. It will return as you want.
With this function you can get all keys from a multidimensional array
What about something like this :
Of course, this is considering all sub-arrays have the same keys ; in this case, you only need the keys of the first sub-array (no need to iterate over all first-level sub-arrays, I guess)
And, as a shortened / simplified example :
Will get you :
You can the use
implode
to get the string you asked for :will get you :
ie, the list of the keys of the first sub-array.
One liner:
Or in a function:
Now lets break this down with an example, here is some sample data:
Starting with the inner most function, we run array_map with the array_keys function:
This gives us the keys of from all child arrays
Then we run the array_reduce on the data with the array_merge callback and an empty array as the initial value:
This converts our multiple arrays into 1 flat array:
Now we strip out our duplicates with array_unique:
And end up with all our keys:
Merge all values and retrieve the resulting keys.