0

This is my first array

Array
(
   [0] => Array
        (
            [0] => 1
            [1] => Elite
            [2] => Air-Con Bus
            [3] => Monday
        )

    [1] => Array
        (
            [0] => 4
            [1] => KBZ
            [2] => Airplane
            [3] => Wednesday
        )
    [2] => Array
        (
            [0] => 5
            [1] => Yoma
            [2] => Cruise
            [3] => Tuesday
        )
)

I want to be inner array[0] to the outer array key. Like the following array: Can I or not? Please suggest me.

Array(
     [1] => Array
        (
            [0] => 1
            [1] => Elite
            [2] => Air-Con Bus
            [3] => Monday
        )

    [4] => Array
        (
            [0] => 4
            [1] => KBZ
            [2] => Airplane
            [3] => Wednesday
        )

    [5] => Array
        (            
            [0] => 5
            [1] => Yoma
            [2] => Cruise
            [3] => Tuesday
        )
)
0

2 Answers 2

1
$new_array = array();
foreach ($old_array as $el) {
    $new_array[$el[0]] = $el;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes. I can do like that! Thanks a lot! :)
I'm really curious: Why isn't this trivial solution obvious? It pretty much comes from your description of the problem: use element[0] as the keys of a new array.
1

one way:

foreach ($array as $a){
$new[$a[0]]=$a;
}

1 Comment

Yes. I can do like that! Thanks a lot! :)

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.