Jegan
Jegan

Reputation: 530

Creating nested parent child array from multi dimensional array in php

I have this array

$array =  Array
    (
        [a] => Array
            (
                [0] => b
                [1] => h
            )

        [b] => c
        [c] => d
        [h] => m
    )

And I need to convert the array to like below

Array
(
    [a] => Array
        (
            [b] => Array
                (
                    [c] => Array
                        (
                            [d] => Array
                                (
                                )

                        )

                )

            [h] => Array
                (
                    [m] => Array
                        (
                        )

                )

        )

)

I already asked this question for One Dimensional array.

I tried with [Creating nested parent child array from one dimensional array in php and I got the below array

Array
(
    [a] => Array
        (
            [b] => Array
                (
                    [a] => Array
                        (
                            [h] => Array
                                (
                                    [b] => Array
                                        (
                                            [c] => Array
                                                (
                                                    [d] => Array
                                                        (
                                                        )

                                                    [h] => Array
                                                        (
                                                            [m] => Array
                                                                (
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

How to Check wheather the key is present in multi dimensional array and if present add the child to the existing key. Help to resolve the Problem. Thanks in Advance

Upvotes: 3

Views: 1218

Answers (1)

Qaisar Satti
Qaisar Satti

Reputation: 2762

<?php 

$array = array( 
   'a' => array(0=>'b',1=>'h'),
   'b' => 'c',
   'c' => 'd',
   'h' => 'm',
);

$newArray    = array();
$secondarray = array();
$part        = &$newArray;
$i=1;

foreach($array as $first => $second)
{
    if($i==1)
    {
        $firstone=$first;
    }
    else
    {
        if($i==count($array))
        {
            $newArray[$first] = array($second => array());
            $secondarray[$firstone]=$newArray;
        }
        else
        {
            $part = &$part[$first];
            $part[$second] = array();
         }
     }
     $i++;
}

echo '<pre>';print_r($secondarray);

output

Array
(
    [a] => Array
        (
            [b] => Array
                (
                    [c] => Array
                        (
                            [d] => Array
                                (
                                )

                        )

                )

            [h] => Array
                (
                    [m] => Array
                        (
                        )

                )

        )

)

Upvotes: 2

Related Questions