AP 2022
AP 2022

Reputation: 787

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

I have an array which has multiple arrays inside of like. Here is how it 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 to figure out is how I can turn these multiple arrays into 1 string where is has values from all arrays split with commas $values = "s1,s2,s5.."

I used impode() before but with this type of array, it's not functioning. Another problem in this is empty arrays which i believe can be removed with array_filter().

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

Upvotes: 0

Views: 2466

Answers (3)

mickmackusa
mickmackusa

Reputation: 47904

The desired result can be achieved a number of ways (beyond Rizier's function approach which is good and clean):

Demo

Method #1: the boring / loopy / fast way

$array = [
    ['s1','s2','s5','s1','s25','s1','s6','s6','s1'],
    2 => ['a2','a1','a4'],
    [],
    []
];
$result = [];
foreach ($array as $subarray) {
    if ($subarray) {
        $result[] = implode(',', $subarray);
    }
}
echo implode(',', $result);

Method #2: the handy "leafnode" grabber

array_walk_recursive(
    $array,
    function($v) {
        static $first;
        echo $first . $v;
        $first = ',';
    }
);

Method #3: the slower, unorthodox regex ways:

echo implode(
         ',',
         preg_match_all(
             '/[a-z]\d+/',
             json_encode($array),
             $out
         )
         ? $out[0]
         : []
     );

And

echo implode(
         ',',
         preg_split(
             '/\W+/',
             json_encode(array_values($array)),
             0,
             PREG_SPLIT_NO_EMPTY
         )
     );

Output (from each of the above:

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

Upvotes: 1

user557846
user557846

Reputation:

I think this will work:

$str='';
foreach($array as $k=>$v){
    if(is_array($v)){
        $str.=implode(',',$v);
    }
}

Upvotes: -1

Rizier123
Rizier123

Reputation: 59691

You have a two dimensional array here. And neither implode() or array_filter() work with multidimensional arrays.

This means you filter all empty values out of the first dimension and also try to implode the 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 and implode it
    )

So obviously what you have to do is, you have to filter each subArray. Then implode each subArray and implode all strings together again.

How can we do this? Just use array_map().

With array_map() you go through each subArray and first filter all empty values out with array_filter(). Then you implode() each subArray to a string. After this you will end up with an array like this:

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

And then you have to implode it again to get 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

Upvotes: 3

Related Questions