7

I have 2 array.

First Array:

$array = array( 
   'a' => 'b',
   'b' => 'c',
   'c' => 'd',
);

Second Array:

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

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

i need to change this array like

First array:

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

                )

        )

)

Second Array:

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

                        )

                )

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

                )

        )

)

sirwilliam answers helped for first array problem. and I need it for multi dimensional array. Help to resolve the problem. Thanks in advance

5
  • And what you have tried.. Commented Jun 10, 2015 at 12:21
  • @Uchiha I tried with array_walk_recursive and used this function function myfunction($products, $value){ foreach($products as $key => $product){ if ( $product === $value ){ return $key; }else{ return $product; } } } Commented Jun 10, 2015 at 12:30
  • But what is $value over here Commented Jun 10, 2015 at 12:32
  • @Jegan you should not post code in comments, you should edit your question. Commented Jun 10, 2015 at 12:32
  • @Uchiha Just passed the array value in the foreach loop Commented Jun 10, 2015 at 12:33

1 Answer 1

8

You can try to use & (references):

PHP:

    $array = array( 
       'a' => 'b',
       'b' => 'c',
       'c' => 'd',
    );

    $newArray = array();
    $newArray[key($array)] = array();
    $part = &$newArray;

    foreach($array as $first => $second){
        $part = &$part[$first];
        $part[$second] = array();
    }

    echo "<pre>";
    print_r($newArray);
    echo "</pre>";

?>

Result:

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

                        )

                )

        )

)

Then you can create a loop for the last part.

Sign up to request clarification or add additional context in comments.

4 Comments

this is always starting at index 'a'... Is this desired behaviour?
What if there is 'cat' => 'over a tree', ?
@sirwilliam If i have multi dimensional array?. Can you help me
If you have a new question ask new question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.