0

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);
0

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

One more thing @Rizier123 with your answer how can i calculate the most common array value? In this case it would result in s1.
@aqq You would merge the arrays together with array_map("array_merge", $array) and then use array_count_values() to get the occurrence of each element, sort it, and then get the one value with the most occurrences
1

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

Comments

-1

I think this will work:

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

1 Comment

This will not put commas between ALL values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.