PHP/MySQL从数据库生成多维关联数组
编辑 我正在尝试创建一个用于测试目的的小CMS。 我已经设置了一个类,该类根据输出如下所示的数组创建带有嵌套元素的导航:PHP/MySQL从数据库生成多维关联数组,php,mysql,Php,Mysql,编辑 我正在尝试创建一个用于测试目的的小CMS。 我已经设置了一个类,该类根据输出如下所示的数组创建带有嵌套元素的导航: Array ( [0] => Array ( [name] => Home [link] => ) [1] => Array ( [name] => About us [link] =
Array
(
[0] => Array
(
[name] => Home
[link] =>
)
[1] => Array
(
[name] => About us
[link] => about
[children] => Array
(
[0] => Array
(
[name] => Team
[link] => team
)
[1] => Array
(
[name] => History
[link] => history
)
)
)
[2] => Array
(
[name] => Contact
[link] => contact
)
)
Array
(
[0] => Array
(
[name] => Home
[link] =>
)
[1] => Array
(
[name] => About us
[link] => about
[children] => Array
(
[0] => Array
(
[name] => Team
[link] => team
)
[1] => Array
(
[name] => History
[link] => history,
[children] => Array
(
[name] => Pictures
[link] => pictures
)
)
)
)
[2] => Array
(
[name] => Contact
[link] => contact
)
)
到目前为止,它工作得相当好,但我最终需要的是一个具有无限嵌套可能性的数组。大概是这样的:
Array
(
[0] => Array
(
[name] => Home
[link] =>
)
[1] => Array
(
[name] => About us
[link] => about
[children] => Array
(
[0] => Array
(
[name] => Team
[link] => team
)
[1] => Array
(
[name] => History
[link] => history
)
)
)
[2] => Array
(
[name] => Contact
[link] => contact
)
)
Array
(
[0] => Array
(
[name] => Home
[link] =>
)
[1] => Array
(
[name] => About us
[link] => about
[children] => Array
(
[0] => Array
(
[name] => Team
[link] => team
)
[1] => Array
(
[name] => History
[link] => history,
[children] => Array
(
[name] => Pictures
[link] => pictures
)
)
)
)
[2] => Array
(
[name] => Contact
[link] => contact
)
)
我使用以下PHP脚本使用数据库中的数据填充数组:
/**
* Loops through the children of a page and adds them accordingly to the pages array
*
* @param array $parent
* @param array $children
*/
private function getChildrenPages($parent, $children) {
$subpages = array();
foreach ($children as $child) {
array_push($subpages, array(
'name' => $child['name'],
'link' => $child['link']
));
}
array_push($this->pages, array(
'name' => $parent['name'],
'link' => $parent['link'],
'children' => $subpages
));
}
/**
* @return array Returns an multidimensional associative array with all pages
*/
private function fetchPages() {
// Prevent multiple db fetches
if(!count($this->pages)){
$all_pages = $this->db->get('pages');
for ($i=0; $i < count($all_pages); $i++) {
$parent = $all_pages[$i];
// Get children of current item
$this->db->where('parent_id', $parent['id']);
$children = $this->db->get('pages');
//
if(count($children)) {
$this->getChildrenPages($parent, $children);
}
if (!$parent['parent_id'] && !count($children)) {
// Append current item without children to pages array
array_push($this->pages, array(
'name' => $parent['name'],
'link' => $parent['link']
));
}
}
}
}
/**
*循环遍历页面的子级,并将它们相应地添加到页面数组中
*
*@param数组$parent
*@param数组$children
*/
私有函数getChildrenPages($parent,$children){
$subpages=array();
foreach($childrenas$child){
数组\u推送($子页,数组(
'name'=>$child['name'],
'link'=>$child['link']
));
}
数组\u推送($此->页面,数组(
'name'=>$parent['name'],
'link'=>$parent['link'],
“children”=>$subpages
));
}
/**
*@return array返回包含所有页面的多维关联数组
*/
私有函数fetchPages(){
//防止多个数据库抓取
如果(!count($this->pages)){
$all_pages=$this->db->get('pages');
对于($i=0;$idb->where('parent_id',$parent['id');
$children=$this->db->get('pages');
//
如果(计算(儿童)){
$this->getChildrenPages($parent,$children);
}
如果(!$parent['parent_id']&&&!count($children)){
//将不带子项的当前项追加到页面数组
数组\u推送($此->页面,数组(
'name'=>$parent['name'],
'link'=>$parent['link']
));
}
}
}
}
这适用于第一级和第二级项目。但是如何处理更高级的项目呢?我想我必须将我的
getChildrenPages()
函数转换为递归函数,但在这种情况下,我不知道如何实现。有什么建议吗?好的,我想你说得对。我过去也做过类似的事情,只是没有让父母列出孩子,而是让孩子列出父母。我想语义上的差别不大,但这会使事情变得容易得多
列出所有没有父项的项。这些将是您的主要导航项目。对于显示的每个项目,检查是否有行将此项目声明为其父项。如果是这样的话,就把它们列在下面。就这些 通过每行存储一个
parent\u id
字段,而不是像这里这样在children
字段中存储多个id,这样存储层次结构通常要容易得多。然后你就可以在有意义的数据上加入,而不必解析字符串了。@Sammitch你能给我举个例子吗?我想,JOIN
用于多个表就像用于两行一样?没有规则禁止将表连接到自身:我不知道为什么fiddle没有显示连接的右侧,但相信我,它就在那里我感谢你的建议。你会如何处理三级菜单?那么让我们假设Team
页面是否会包含另一个子页面?我所做的只是将父级设置为第二级子行的id。想象一下,如果a
,b
,c
,是菜单项,aa
&bb
将其父母列为b
。然后您可以将aaa
和bbb
列为拥有父bb
。听起来很合理。你能给我看看你的一些代码吗?我修改了我的代码和数据库方案,并重写了我的问题。请看一看。