How do I convert a multidimensional array to a two-dimensional array?
For example, an infinite menu, after recursion is a multidimensional array, I want to convert this multidimensional array into a two-dimensional array and re-sort after the foreach, how can I write this function?
------Solution--------------------
$a = Array (
0=>array (
' CategoryID ' =>1,
' Name ' = ' AA1 ',
' Parent ' =>0,
' Childs ' =>array (
0=>array (
' CategoryID ' =>2,
' Name ' = ' BB1 ',
' Parent ' =>1,
' Childs ' =>array (
0=>array (
' CategoryID ' =>3,
' Name ' = "CC",
' Parent ' =>2,
' Childs ' =>array (
0=>array (
' CategoryID ' =>4,
' Name ' = ' dd1 ',
' Parent ' =>3,
),
1=>array (
' CategoryID ' =>5,
' Name ' = ' DD2 ',
' Parent ' =>3,
),
),
),
),
),
1=>array (
' CategoryID ' =>6,
' Name ' = ' BB2 ',
' Parent ' =>1,
),
),
),
1=>array (
' CategoryID ' =>7,
' Name ' = ' AA2 ',
' Parent ' =>0,
)
);
function imp ($tree, $children = ' childs ') {
$imparr = Array ();
foreach ($tree as $w) {
if (Isset ($w [$children])) {
$t = $w [$children];
Unset ($w [$children]);
$imparr [] = $w;
if (Is_array ($t)) $imparr = Array_merge ($imparr, IMP ($t, $children));
} else {
$imparr [] = $w;
}
}
return $imparr;
}
Print_r (Imp ($a));
array
(
[0] = = Array
(
[CategoryID] = 1
[Name] = AA1
[parent] + 0
)
[1] = = Array
(
[Cat Egoryid] = 2
[name] = BB1
[parent] + 1
)
[2] = = Array (
[CategoryID] = 3
[name] + CC
[Parent] = 2
)
[3] = = Array
(
[CategoryID] + 4
[name] = + DD1
[Parent] = 3
)
[4] = = Array
(
[CategoryID] = 5