I have an array like:
Array
(
[0] => Array
(
[0] => a
[1] => b
)
[1] => Array
(
[0] => c
)
[2] => Array
(
[0] => d
[1] => e
[2] => f
)
)
I want to convert my array to a string like below:
$arrtostr = 'a,b,c,d,e,f';
I've used implode()
function but it looks like it doesn't work on two-dimensional arrays.
What should I do?
You asked for a two-dimensional array, here's a function that will work for multidimensional array.
I can flatten an array structure like so:
The results is:
Given your subject array:
Two easy ways to get a "flattened" array are:
PHP 5.6.0 and above using the splat operator:
Lower than PHP 5.6.0 using
call_user_func_array()
:Both of these give an array like:
Then to get your string, just implode:
Alternatively, you could use a container for that first, merge the contents, and in the end of having a flat one, then use
implode()
:Sample Output