php - How can I implode() a multidimensional array into a string? -


i have array has multiple arrays inside of like. here how looks like:

array (     [0] => array (         [0] => s1         [1] => s2         [2] => s5         [3] => s1         [4] => s25         [5] => s1         [6] => s6         [7] => s6         [8] => s1     )     [2] => array (         [0] => a2         [1] => a1         [2] => a4     )     [3] =>  array ( )     [4] =>  array ( ) ) 

what i'm trying figure out how can turn these multiple arrays 1 string has values arrays split commas $values = "s1,s2,s5.."

i used impode() before type of array, it's not functioning. problem in empty arrays believe can removed array_filter().

$destination_array = array_filter($tags_list); $destination_array = implode(",", $tags_list); print_r($destination_array); 

you have 2 dimensional array here. , neither implode() or array_filter() work multidimensional arrays.

this means filter empty values out of first dimension , try implode first dimension:

array (         [0] => array (             [0] => s1             [1] => s2             [2] => s5             [3] => s1             [4] => s25             [5] => s1             [6] => s6             [7] => s6             [8] => s1         )         [2] => array (             [0] => a2             [1] => a1             [2] => a4         )         [3] =>  array ( )         [4] =>  array ( )          ↑ filter first dimension , implode it     ) 

so have is, have filter each subarray. implode each subarray , implode strings again.

how can this? use array_map().

with array_map() go through each subarray , first filter empty values out array_filter(). implode() each subarray string. after end array this:

array (     [0] => s1,s2,s5,s1,s25,s1,s6,s6,s1     [2] => a2,a1,a4 ) 

and have implode again 1 string out of it.

code:

echo implode(",", array_filter(array_map(function($v){          return implode(",", array_filter($v));      }, $array))); 

output:

s1,s2,s5,s1,s25,s1,s6,s6,s1,a2,a1,a4 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -