在多维数组的数组项存在 [英] Array key exists in multidimensional array

查看:184
本文介绍了在多维数组的数组项存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个数组,安排一组页面并将它们依赖于他们的父母ID号。如果父ID为0,我想它被放置在阵列中作为这样一个数组...

I'm trying to arrange a group of pages in to an array and place them depending on their parent id number. If the parent id is 0 I would like it to be placed in the array as an array like so...

$get_pages = 'DATABASE QUERY'
$sorted = array()

foreach($get_pages as $k => $obj) {
    if(!$obj->parent_id) {
        $sorted[$obj->parent_id] = array();
    }
}

但是,如果父ID设为我想再次将其放置在相关数组,这样一个数组...

But if the parent id is set I'd like to place it in to the relevant array, again as an array like so...

$get_pages = 'DATABASE QUERY'
$sorted = array()

foreach($get_pages as $k => $obj) {
    if(!$obj->parent_id) {
        $sorted[$obj->id] = array();
    } else if($obj->parent_id) {
        $sorted[$obj->parent_id][$obj->id] = array();
    }
}

这是我开始有问题。如果我有一个需要插入到需要在第三维我没有如果数组键存在的检查方式插入阵列,甚至是第4单元的第二维第3元。所以,我无法弄清楚是如何在第一维后的数组项存在,并且如果是这样的地方是这样我就可以把新的元素检测。

This is where I begin to have a problem. If I have a 3rd element that needs to be inserted to the 2nd dimension of an array, or even a 4th element that needs inserting in the 3rd dimension I have no way of checking if that array key exists. So what I can't figure out is how to detect if an array key exists after the 1st dimension and if it does where it is so I can place the new element.

下面是我的数据库表的一个例子

Here is an example of my Database Table

id    page_name    parent_id

1     Products             0
2     Chairs               1
3     Tables               1
4     Green Chairs         2
5     Large Green Chair    4
6     About Us             0

下面是我想要得到的,如果有更好的方法来做到这一点我很开放的建议输出的例子。

Here is an example of the output I'd like to get, if there is a better way to do this I'm open for suggestions.

Array([1]=>Array([2] => Array([4] => Array([5] => Array())), [3] => Array()), 6 => Array())

在先进的感谢!

推荐答案

好了,基本上你正在建设一个树所以去的方法之一就是用的递归

Well, essentially you are building a tree so one of the ways to go is with recursion:

// This function takes an array for a certain level and inserts all of the 
// child nodes into it (then going to build each child node as a parent for
// its respective children):

function addChildren( &$get_pages, &$parentArr, $parentId = 0 )
{
    foreach ( $get_pages as $page )
    {
        // Is the current node a child of the parent we are currently populating?

        if ( $page->parent_id == $parentId )
        {
            // Is there an array for the current parent?

            if ( !isset( $parentArr[ $page->id ] ) )
            {
                // Nop, create one so the current parent's children can
                // be inserted into it.

                $parentArr[ $page->id ] = array();
            }

            // Call the function from within itself to populate the next level
            // in the array:

            addChildren( $get_pages, $parentArr[ $page->id ], $page->id );
        }
    }
}


$result = array();
addChildren( $get_pages, $result );

print_r($result);

这是不去,但最有效的方式为一小的页数&放大器;层次结构,你应该罚款。

This is not the most efficient way to go but for a small number of pages & hierarchies you should be fine.

这篇关于在多维数组的数组项存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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