我有一些类别需要转换为嵌套数组(树)。我使用物化路径来制作一棵树,我在 PHP 中工作。这是我所拥有的 print_r:
Array
(
[0] => Array
(
[_id] => mac
[name] => Mac
[path] => null
)
[1] => Array
(
[_id] => ipod
[name] => iPod
[path] => null
)
[2] => Array
(
[_id] => imac
[name] => iMac
[path] => ,mac,
)
[3] => Array
(
[_id] => imac2001
[name] => iMac 2001
[path] => ,mac,imac,
)
[4] => Array
(
[_id] => imac2002
[name] => iMac 2002
[path] => ,mac,imac,
)
[5] => Array
(
[_id] => imac2003
[name] => iMac 2003
[path] => ,mac,imac,
)
)
我试图做到这一点,但我迷失了递归函数的整个想法。
更新 :
这是我最终想要的:
Array
(
[0] => Array
(
[_id] => mac
[name] => Mac
[path] => null
[children] =>
[0] => Array
(
[_id] => imac
[name] => iMac
[path] => ,mac,
[children] =>
[0] => Array
(
[_id] => imac2001
[name] => iMac 2001
[path] => ,mac,imac,
)
[1] => Array
(
[_id] => imac2002
[name] => iMac 2002
[path] => ,mac,imac,
)
[2] => Array
(
[_id] => imac2003
[name] => iMac 2003
[path] => ,mac,imac,
)
)
)
[1] => Array
(
[_id] => ipod
[name] => iPod
[path] => null
)
)
我想向您展示我已经编码的内容,但它离工作还很远,所以在我看来没什么好看的。