0
Array(
 [0] => Array
        (
            [0] => 2017-10-05
            [1] => 24,57
            [2] => 24,65
            [3] => 23,86
        )

    [2] => Array
        (
            [0] => 2017-10-04
            [1] => 24,38
            [2] => 24,675
            [3]=> 24,24
        )
   )

I want to convert this into

Array(
 [2017-10-05] => Array
        (

            [0] => 24,57
            [1] => 24,65
            [2] => 23,86
        )

    [2017-10-04] => Array
        ( 
            [0] => 24,38
            [1] => 24,675
            [2] => 24,24
        )

This question similar to How to make array first value as key for the second value as value in php array. But i can't use array_column in my case,

1 Answer 1

6

You can use this code with array_slice:

$newArray = [];
foreach($array as $a) {
    $newArray[$a[0]] = array_slice($a, 1); 
}

DEMO

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

5 Comments

Good Solution, I am trying to do this with array_walk, But i think this is better and simpler , Thanks
Glad to hear that. Welcome.
Btw another version of this would be $newArray = []; foreach($array as $a) { $first_ele = array_shift($a); $newArray[$first_ele] = $a;} `
Yeah, I would even shorten it by one line foreach($array as $a) { $newArray[array_shift($a)] = $a;}
And if golfing, do away with the curlies.

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.