Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将数组从平面(子数组存储父数组索引的位置)重新创建为多维数组?_Php_Arrays_Multidimensional Array - Fatal编程技术网

Php 将数组从平面(子数组存储父数组索引的位置)重新创建为多维数组?

Php 将数组从平面(子数组存储父数组索引的位置)重新创建为多维数组?,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我正在尝试创建一个平面数组,并重新创建它,使其具有多维性。我一直在研究array_combine和array_merge,但我不确定这两种产品是否能满足我的期望 数组的当前形式(这只是一个简化的示例): 我试图创建一个多维数组,其中每个对象都是其父对象的子数组。因此,在上面的示例中,我尝试输出: Array ( [0] => stdClass Object ( [tid] => 31 [name] => S

我正在尝试创建一个平面数组,并重新创建它,使其具有多维性。我一直在研究array_combine和array_merge,但我不确定这两种产品是否能满足我的期望

数组的当前形式(这只是一个简化的示例):

我试图创建一个多维数组,其中每个对象都是其父对象的子数组。因此,在上面的示例中,我尝试输出:

Array
(
    [0] => stdClass Object
        (
            [tid] => 31
            [name] => Safeway
            [children] => Array
                (
                    [tid] => 32
                    [name] => Dairy
                    [children] => Array
                        (
                            [tid] => 33
                            [name] => Milk
                        )
                )

        )
)

好的,我在这里做一些假设:

  • 每个元素只有一个父元素,因此
    父元素
    数组只有一个tid
  • 数组的排序方式是,子数组仅出现在其父数组之后
  • 顶级elmenets的父级为0
鉴于此,请尝试以下代码:

$original = array ( ... your original array ... );
$nested = array ();

$n = count($original);
for ($i = 0; $i < $n; ++$i)
{
    $nested[$original[$i]->tid] = $original[$i];
    $nested[$original[$i]->tid]->children = array ();
}

while ($n-- && $current = $original[$n])
    if ($current->parents[0] != 0 && $current->parents[0] != $current->tid)
    {
        $nested[$current->parents[0]]->children[] = $current;
        unset ($nested[$current->tid]);
    }
$original=array(…您的原始数组…);
$nested=array();
$n=计数(原件);
对于($i=0;$i<$n;++$i)
{
$nested[$original[$i]->tid]=$original[$i];
$nested[$original[$i]->tid]->children=array();
}
而($n--&&&$current=$original[$n])
如果($current->parents[0]!=0&&$current->parents[0]!=$current->tid)
{
$nested[$current->parents[0]]->childrent[]=$current;
取消设置($nested[$current->tid]);
}

首先,您显示的不是多维数组,而是StdClass对象数组

如果您可以让它们成为真正的阵列,那么这可能会做到:

// initiate result array
$multiArray = array();

// assume $items is your current array
foreach( $items as $item )
{
    // cast StdClass to array
    $objToArray = (array) $item;

    // if this item is initiated already merge it with the current item
    $multiArray[ $objToArray[ 'tid' ] ] = isset( $multiArray[ $objToArray[ 'tid' ] ] ) ? $multiArray[ $objToArray[ 'tid' ] ] + $objToArray : $objToArray;

    foreach( $objToArray[ 'parents' ] as $parentId )
    {
        // if parents don't exist yet, initiate them
        if( !isset( $multiArray[ $parentId ] ) )
        {
            $multiArray[ $parentId ] = array(
                'children' => array()
            );
        }

        // add this item to the parents children collection by reference (for efficiency)
        $multiArray[ $parentId ][ 'children' ][ $objToArray[ 'tid' ] ] = &$multiArray[ $objToArray[ 'tid' ] ];
    }
}
使用此功能,您可以通过以下id轻松查找项目:

$item = $multiArray[ $someId ];
为了得到一个孩子:

$child = $item[ 'children' ][ $someChildId ];
或所有儿童:

$children = $item[ 'children' ];
编辑
好的,我现在已经测试过了,在添加了一些缺少的分号之后,它似乎工作得很好

$children = $item[ 'children' ];