我正在检索自定义CMS的数据库中的所有页面。这些页面是嵌套的并且具有parent_id以查找父页面的子项。Array中的嵌套页面 - PHP
代码检索代码:
public function get_nested()
{
$pages = $this->db->get('pages')->result_array();
$array = array();
foreach ($pages as $page)
{
if (!$page['parent_id'])
{
$array[$page['id']] = $page;
}
else
{
$array[$page['parent_id']]['children'][] = $page;
}
}
return $array;
}
出于某种原因,其他条件不使用$阵列[$页[ 'PARENT_ID']工作。
的$页面转储给我
Array
(
[0] => Array
(
[id] => 1
[title] => Homepage
[slug] =>/
[order] => 1
[body] => Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat.
[parent_id] => 0
)
[1] => Array
(
[id] => 3
[title] => Contact
[slug] => contact
[order] => 0
[body] => <p>This is my contact page.</p>
[parent_id] => 4
)
[2] => Array
(
[id] => 4
[title] => About
[slug] => about
[order] => 0
[body] => <p>All about our company.</p>
[parent_id] => 0
)
)
所以我期待的接触出现在条件的其他部分。我试过在条件中只回显'parent'和'child',它有效,但是在写$ array [$ page ['parent_id']]时什么都没有出现,甚至没有一个块说它是空的。 目前我得到的回应是:
Array
(
[1] => Array
(
[id] => 1
[title] => Homepage
[slug] =>/
[order] => 1
[body] => Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat.
[parent_id] => 0
)
[4] => Array
(
[id] => 4
[title] => About
[slug] => about
[order] => 0
[body] => <p>All about our company.</p>
[parent_id] => 0
)
)
有谁知道为什么$阵列[$页[“PARENT_ID”]没有做任何事情?我认为它会给我一个[4]内的儿童数组,但没有任何内容。我检查了数据库中parent_id的类型,它是INT(11),所以不应该造成问题。
在此先感谢。
在MySQL内部分层的数据是很难实现的根本。你涉及的表格的模式是什么?页面是否具有确定的深度,或者它们是否能够嵌套* ad infinitum *? – BenM 2013-03-01 18:06:14
嗨BenM,感谢您的快速回复。我在这里添加了数据库的视觉效果:[链接](http://i1097.photobucket.com/albums/g351/europcsolutions/sql_zps2d3c3b19.jpg) 理论上,在MySQL中,页面可以具有无限深度,但这是在PHP中处理,当添加一个新页面时,你只能选择深度为0的父母。所以最大深度真的是1. – xonorageous 2013-03-01 18:14:53