How can I return multidimensional array keys in tree format in PHP?
For example, if I have the following array:
$array = array (
array (
'name' => 'A',
'product' => array (
'qty' => 1,
'brand' => 'Tim'
),
'goods' => array (
'qty' => 2
),
'brand' => 'Lin'
),
array (
'name' => 'B',
'product' => array (
'qty' => 6,
'brand' => 'Coff'
),
'goods' => array (
'qty' => 4
),
'brand' => 'Ji'
)
);
How can I get a result like the following — including no repeating of keys:
-name
-product
--qty
--brand
-goods
--qty
--brand
With an unlimited depth you need a recursive function. I suppose you have the parents in $names and the childs in $children:
This functions use useful because you can feed it with 2 variables. The other answere requires a multidimensional array.