After implementing database queries, I am getting the multi-dimensional array below.
Two Dimensional Array
Array
(
[0] => Array
(
[t1] => test1
)
[1] => Array
(
[t2] => test2
)
[2] => Array
(
[t3] => test3
)
[3] => Array
(
[t4] => test4
)
[4] => Array
(
[t5] => test5
)
)
but I want to convert it to a single dimensional array, like the format below.
One Dimensional Array
Array (
t1 => test1
t2 => test2
t3 => test3
t4 => test4
t5 => test5
)
Please help me to do so
You can use
array_reduce()
to change values of array. In callback get key of item usingkey()
and select first item usingreset()
.Check result in demo
Try this function,
Give it a try, it will work.
You can try traversing the array using PHP
while
list
andeach
. I took sample code from PHP website the second example you can check it hereOutput created is
You can test it on your own in this Sandbox example.
For your specific case, i would use
array_reduce
where i set the init value by an empty arrayResult :