I have this array in PHP:
Array ( [0] => Array ( [time_book_start] => 11:30 )
[1] => Array ( [time_book_start] => 9:00 )
)
I just need to output 11:30 and 9:00 in this manner:
['11:30','9:00']
I have used json_encode
, sprintf
, implode
etc without success. Please help me.
2
Answers
The desired result doesn’t match the structure of your array, so first you need to change the array structure. Then you can encode it as JSON to get that formatted output.
One way is to simply construct a new array by taking data from the original one:
Demo: https://3v4l.org/lF7LL
However PHP also has a built-in function
array_column()
which can do the same job for you in this particlular scenario. It returns all the values from the named column in the array, in a single flat array structure:Demo: https://3v4l.org/VgF4Q
You can use the
array_map
function in PHP.Or more shortly you can also use
array_column
function.