我有一个多维数组,它有父子关系。
我希望将其显示为以适当顺序表示父母的数值列表。
Array
(
[0] => Array
(
[name] => Category 1
[id] => 6
[children] => Array
(
[0] => Array
(
[name] => Category 2
[id] => 7
[children] => Array
(
[0] => Array
(
[name] => cricket1
[id] => 19
[children] => Array
(
[0] => Array
(
[name] => cricket2
[id] => 21
[children] =>
)
)
)
)
)
[1] => Array
(
[name] => another category
[id] => 15
[children] => Array
(
[0] => Array
(
[name] => new 1
[id] => 18
[children] =>
)
)
)
)
)
[1] => Array
(
[name] => Category 3
[id] => 8
[children] => Array
(
[0] => Array
(
[name] => new category
[id] => 11
[children] =>
)
)
)
[2] => Array
(
[name] => Sazzad
[id] => 9
[children] => Array
(
[0] => Array
(
[name] => Mahmud
[id] => 10
[children] =>
)
)
)
)
我想在下面给出这个数组:
1. Category 1 1.1 Category 2 1.1.1 cricket1 1.1.1.1 cricket2 1.2 another category 1.2.1 new 1 2. Category 3 2.1 new category 3. Sazzad 3.1 Mahmud
如何使用PHP实现此编号系统?
提前致谢。
答案 0 :(得分:1)
您可以使用简单的递归函数:
function makeOneLevelArray($in, &$out, $prefixLevel = '') {
$level = 1;
foreach ($in as $v) {
$out[] = $prefixLevel . $level . ' ' . $v['name'];
if (!empty($v['children'])) {
makeOneLevelArray($v['children'], $out, $prefixLevel . $level . '.');
}
$level++;
}
}
$outputArray = array();
makeOneLevelArray($inputArray, $outputArray);
print_r($outputArray);