1

This is my array:

Array
(
    [0] => Array
        (
            [0] => Q1
            [1] => 100
        )

    [1] => Array
        (
            [0] => Q2
            [1] => 200
        )

    [2] => Array
        (
            [0] => Q3
            [1] => 300
        )

)

I want to have an array like this:

Array
 (
[Q1] => 100
[Q2] => 200
[Q3] => 300
)

So basically I want to split all arrays in one, and 0 key of all multi arrays will be key in the new array and 1 value in multi-array will be value in the new array. I tried with array_combine, but that does not work for me, any ideas?

0

2 Answers 2

2

There's a function for that:

$result = array_column($array, 1, 0);

Failing that just loop:

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

9 Comments

Using this, Ive got this: Array ( [0] => 100 [1] => 200 [2] => 300 ) And I want this:
Array ( [Q1] => 100 [Q2] => 200 [Q3] => 300 )
That was not your question at all. Where does Q1, Q2 etc. come from?
they are the 0 values in multi-arrays, please see again the question
Yes, and that's exactly what the code does, did you try it?
|
1

Use this straight-forward solution :

$arr = [
  ['Q1',100],
  ['Q2',200],
  ['Q3',300]
];

$res = array_combine(
                      array_column($arr, 0), 
                      array_column($arr, 1)
                    );

print_r($res);

Comments