PHP阵列 - 如何一维数组嵌套的多维数组? [英] PHP arrays - How to 1-dimensional array into nested multidimensional array?

查看:112
本文介绍了PHP阵列 - 如何一维数组嵌套的多维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当检索从MySQL的分层结构(具有一个ID列和一个PARENT列标志着层次关系表),我的结果映射到如下枚举阵列(对于本例的数字是任意的):

When retrieving a hierarchical structure from MySQL (table with one ID column and one PARENT column signifying the hierarchical relationships), I map the result into an enumerated array as follows (for this example the numbers are arbitrary):

Array ( [3] => Array ( [7] => Array () ), [7] => Array ( [8] => Array () ) )

3的通知是7父母和7是8的父(这可能会继续下去;而任何父母可以有多个子女)

Notice 3 is the parent of 7, and 7 is the parent of 8 (this could go on and on; and any parent could have multiple children).

我想这个数组收缩成一个嵌套的多维数组如下:

I wanted to shrink this array into a nested multidimensional array as follows:

Array ( [3] => Array ( [7] => Array ( [8] => Array () ) ) )

也就是说,每一个新的ID会自动分配一个空数组。无论如何,任何ID的孩子将被推入他们的父母的数组。

在下面的图示进一步澄清请看下图:

Take a look at the following illustration for further clarification:

这可能会导致一个复杂的递归操作,因为我总是要检查与任何特定的ID父母是否已经存在(如果有的话,推值到它的数组)。

This will probably result in a complicated recursive operation, since I always have to check whether a parent with any certain ID already exists (and if so, push the value into its array).

有一个内置的PHP函数,可以帮助我呢?你有没有为如何去构建这个任何想法?对于什么是值得我字preSS采用这种内置的导航栏(可包含类别,子类别,职位......本质上的任何东西)。

Is there a built-in php function that can assist me with this? Do you have any idea as to how to go about constructing this? For what it's worth I'm using this to built a navigation bar in wordpress (which can contain categories, subcategories, posts... essentially anything).

推荐答案

这个想法是,你保持一个辅助阵列,你找到的节点(父和子)。该阵列的值是备份你的结果的引用。

The idea is that you keep an auxiliary array with all the nodes (parent and child) you find. The values of this arrays are references that back your result.

这在建立线性时间的树(array_key_exists做了哈希表查找,这是平均O(1)):

This builds the tree in linear time (array_key_exists does a hash table lookup, which is on average O(1)):

//table contains (id, parent)
$orig = array(
    11 => 8,
    7 => 3,
    8 => 7,
    99 => 8,
    16 => 8,
);

$childrenTable = array();
$result = array();

foreach ($orig as $n => $p) {
    //parent was not seen before, put on root
    if (!array_key_exists($p, $childrenTable)) {
        $childrenTable[$p] = array();
        $result[$p] = &$childrenTable[$p];
    }
    //child was not seen before
    if (!array_key_exists($n, $childrenTable)) {
        $childrenTable[$n] = array();
    }

    //root node has a parent after all, relocate
    if (array_key_exists($n, $result)) {
        unset($result[$n]);
    }

    $childrenTable[$p][$n] = &$childrenTable[$n];
}
unset($childrenTable);

var_dump($result);

array(1) {
  [3]=>
  array(1) {
    [7]=>
    array(1) {
      [8]=>
      array(3) {
        [11]=>
        array(0) {
        }
        [99]=>
        array(0) {
        }
        [16]=>
        array(0) {
        }
      }
    }
  }
}

编辑:未设置 $ childrenTa​​ble 到底要明确的参考标志。在实践中,你可能会想要做一个函数内部操作呢。

unset $childrenTable in the end to clear reference flags. In practice, you will probably want to do the operation inside a function anyway.

这篇关于PHP阵列 - 如何一维数组嵌套的多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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