Reputation: 519
I am trying to get the assosiation names of my array into the first index of the array without having to repeat the 50 "columns"
Is that possibles. I was looking at
array_combine, array_merge, array_fill_keys.
But somehow that does seem to be the way I would want it. I can´t find the right function.
Array example:
loop some queryresult
$array1 = array(
"a" => $first,
"b" => $second,
"c" => $third
}
Desired Output:
Array(
[0] => Array(
[a] => a,
[b] => b,
[c] => c
),
[1] => Array(
[a] => first,
[b] => second,
[c] => third
),
Upvotes: 0
Views: 54
Reputation: 996
There's no way without iterating over the array keys.
$output = array(array(), $array1);
foreach(array_keys($array1) as $key)
$output[0][$key] = $key;
Upvotes: 0
Reputation: 72256
It can be easily accomplished using array_keys()
and array_combine()
:
$input = array(
'a' => 'first',
'b' => 'second',
'c' => 'third',
);
$output = array(
array_combine(array_keys($input), array_keys($input)),
$input,
);
Read about array_keys()
and array_combine()
.
Upvotes: 1