1

I have a question here. Let's say I have an array of the form:

Array
(
     [0] => Array
         (
             [0] => Array
                 (
                     [A] => Array
                         (
                             [id] => 1
                             [firstname] => John
                             [lastname] => Smith
                             [email] => [email protected]
                         )

                     [B] => Array
                         (
                         )

                 )

         )

     [1] => Array
         (
             [0] => Array
                 (
                     [A] => Array
                         (
                             [id] => 2
                             [firstname] => Tommy
                             [lastname] => Tom
                             [email] => [email protected]
                         )

                     [B] => Array
                         (
                         )

                 )

         )

)

How can I replace the index of the outer array by the index of the inner array in order to have an array like this:

Array
(
        [0] => Array
            (
                [A] => Array
                    (
                        [id] => 1
                        [firstname] => John
                        [lastname] => Smith
                        [email] => [email protected]
                    )

                [B] => Array
                    (
                    )

            )



        [1] => Array
            (
                [A] => Array
                    (
                        [id] => 2
                        [firstname] => Tommy
                        [lastname] => Tom
                        [email] => [email protected]
                    )

                [B] => Array
                    (
                    )

            )



)

Thanks in advance!

2
  • Not sure why anyone downvoted this, seems like a reasonable question to me. Commented Jan 16, 2012 at 16:37
  • The difference I can see in the two is that third level array becomes the second level array. Have you tried to simply go: unusualArray[0] = unusualArray[0][0]? Commented Jan 16, 2012 at 16:42

3 Answers 3

3

Another option, for your particular case, could be as simple as:

$out = array_map('reset', $in);
Sign up to request clarification or add additional context in comments.

1 Comment

+1, good thinking! Ruby being my main language, I really should have thought of this one =).
2

You have to loop through the arrays and create a new array based on the inner values.

$in_array = <your array>;
$out_array = Array();

foreach($in_array as $k => $v) {
    $out_array[$k] = array_shift($v);
}

Here, $out_array[$k] keeps the original top-level array keys, and array_shift($v) says to dig down one level for the values (taking the value at the first element in the mid-level array using array_shift, and applying it as the value for the new array).

Comments

-1
//$array is your array
foreach($array as $key=>$value)
{
   $new_array[] = $array[$key];
}

Comments

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.