Easiest Way to Implode() a Two-Dimensional Array

Easiest way to implode() a two-dimensional array?

This modifies your array using array_map, but probably for the better by turning it into a 1D array of tag_id's. Then you can just use implode like normal:

$arr = array_map(function($el){ return $el['tag_id']; }, $arr);
$str = implode(',', $arr);

If you don't want to modify your array than you can just do this:

$str = implode(',', array_map(function($el){ return $el['tag_id']; }, $arr));

Codepad Demo

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

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

Implode data from a Two-dimensional array

Simple solution using array_walk_recursive function:

// $arr is your initial array
$new_arr = [];
array_walk_recursive($arr, function($v) use(&$new_arr){ $new_arr[] = $v; });

DEMO link

http://php.net/manual/en/function.array-walk-recursive.php

How to implode subarrays in a 2-dimensional array?

Just create a new array and set the elements (-;

<?php
...
$new_array = [];
foreach ($my_array as $key => $value)
$new_array[$key] = is_array($value) ? implode(",", $value) : $value;

Implode a column of values from a two dimensional array

Simplest way, when you have only one item in inner arrays:

$values = array_map('array_pop', $array);
$imploded = implode(',', $values);

EDIT: It's for version before 5.5.0. If you're above that, see better answer below :)

Implode two-dimensional array in PHP

Alternatively, you could use a container for that first, merge the contents, and in the end of having a flat one, then use implode():

$letters = array();
foreach ($array as $value) {
$letters = array_merge($letters, $value);
}

echo implode(', ', $letters);

Sample Output

Return single column from a multi-dimensional array

Quite simple:

$input = array(
array(
'tag_name' => 'google'
),
array(
'tag_name' => 'technology'
)
);

echo implode(', ', array_map(function ($entry) {
return $entry['tag_name'];
}, $input));

http://3v4l.org/ltBZ0


and new in php v5.5.0, array_column:

echo implode(', ', array_column($input, 'tag_name'));

Best way to implode multidimensional array

If it is a two dimensional array and would like to output all elements, you could use a foreach loop and output the implode of each like so:

$mainArray = [
[4],
[3, 4],
[2, 30, 43, 65, 53, 634]];

foreach($mainArray as $key => $secArray){
echo implode('-', $secArray) . '<br/>';
}

PHP Implode

Notice the return type of implode is a string.



Related Topics



Leave a reply



Submit