Recover array from apartment (where child arrays store parent array index) in multidimensional?

I am trying to take a flat array and recreate it so that it is multidimensional. I studied array_combine and array_merge, but I'm not sure if any of them will give me what I hope for ...

An array, the current form in it (and this is just a simplified example):

Array
(
    [0] => stdClass Object
        (
            [tid] => 31
            [name] => Safeway
            [parents] => Array
                (
                    [0] => 0
                )

        )

    [1] => stdClass Object
        (
            [tid] => 32
            [name] => Dairy
            [parents] => Array
                (
                    [0] => 31
                )

        )

    [2] => stdClass Object
        (
            [tid] => 33
            [name] => Milk
            [parents] => Array
                (
                    [0] => 32
                )

        )
)

I am trying to create a multidimensional array where each object is a subarray of its parent. So, in the example above, I am trying to deduce:

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

        )
)
+2
source share
2 answers

First, you are not showing a multidimensional array, but an array of StdClass objects.

If it’s good for you to make them truly arrays, this can do it:

// 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' ];


, , , , .

+1

, :

  • 1 , parents 1 tid
  • , .
  • parent = 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]);
    }
0

Source: https://habr.com/ru/post/1729078/


All Articles