PHP:循环通过多维数组,并建立阵列项目之间的父子关系 [英] PHP: Loop through multidimensional array and establish parent-child relationships between array items

查看:768
本文介绍了PHP:循环通过多维数组,并建立阵列项目之间的父子关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个内容管理系统,我已经运行到与在CMS项目的父子关系的问题。

I am developing a content management system and I have run into an issue with child-parent relationships of items in the CMS.

基本上我有一个可以创建页面并创建一个页面时,你可以选择分导航父页面的系统。这是所有罚款和花花公子,直到我尝试生成从数据库导航。

Basically I have a system that can create pages and when a page is created you can select a parent page for sub-navigation. This is all fine and dandy until I try to generate the navigation from the DB.

我不知道,如果某种加盟会更好,但我preFER获得数组中的所有数据和操作使用PHP的数组。

I'm not sure if some sort of join would be better but I prefer to get all the data in an array and manipulate the array with php.

我从DB结果数组是这样的:

My array of results from the DB is like this:

Array
(
    [0] => Array
    (
        [id] => 27
        [name] => home
        [link] => home.html
        [parent] => 0
    )

    [1] => Array
    (
        [id] => 30
        [name] => about
        [link] => about.html
        [parent] => 27
    )
)

我需要遍历这样的数组,可以有任意数量的导航和智能排序它到它的父子关系。我能做到这一点,但只有深一个层次。它需要与儿童等使用图层并输出到HTML无序列表嵌套无限多的孩子来管理孩子。

I need to loop through an array like this that can have any number of navigation and intelligently sort it into its parent child relationships. I was able to do it but only one level deep. It needs to manage children with children with children etc. with an infinite number of layers and output it to HTML unordered nested lists.

<ul>
  <li>
  <a>Nav</a>
     <ul>
        <li>
           <a>Subnav</a>
             <ul>
                 <li>
                    <a>Sub Sub nav</a>
                 </li>
             </ul>
        </li>
     </ul>
  <li>
<ul>

等。 ...

推荐答案

我不认为你应该进入的对象。另外,我认为这纯粹是额外的工作来生成对象和等。在我看来,你应该通过数组循环,并生成一个多维数组重新presents导航层次结构,然后循环所生成的数组递归来生成你的HTML。我已经做了抽样code对你来说,它的工作原理你想要的方式它,但你可能要做出一些改变。

I don't think you should get into objects. Plus I think it would just be extra work to generate objects and etc. In my opinion you should loop through the array and generate a multidimensional array that represents the navigational hierarchy and then loop the generated array recursively to generate your HTML. I've done a sample code for you, it works the way you want it to but you probably want to make some changes.

功能

// Generate your multidimensional array from the linear array
function GenerateNavArray($arr, $parent = 0)
{
    $pages = Array();
    foreach($arr as $page)
    {
        if($page['parent'] == $parent)
        {
            $page['sub'] = isset($page['sub']) ? $page['sub'] : GenerateNavArray($arr, $page['id']);
            $pages[] = $page;
        }
    }
    return $pages;
}

// loop the multidimensional array recursively to generate the HTML
function GenerateNavHTML($nav)
{
    $html = '';
    foreach($nav as $page)
    {
        $html .= '<ul><li>';
        $html .= '<a href="' . $page['link'] . '">' . $page['name'] . '</a>';
        $html .= GenerateNavHTML($page['sub']);
        $html .= '</li></ul>';
    }
    return $html;
}

**样品使用**

** sample usage **

$nav = Array
(
    Array
    (
        'id' => 27,
        'name' => 'home',
        'link' => 'home.html',
        'parent' => 0
    ),
    Array
    (
        'id' => 30,
        'name' => 'about',
        'link' => 'about.html',
        'parent' => 27
    )
);

$navarray = GenerateNavArray($nav);
echo GenerateNavHTML($navarray);

您也许可以做两件事一步到位,但我认为这是整洁首先生成多维数组。古德勒克!

You can probably do both things in one step but I think it's neater to generate the multidimensional array first. Goodluck!

这篇关于PHP:循环通过多维数组,并建立阵列项目之间的父子关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆