Multidimensional Array PHP Implode

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.

Multidimensional Array PHP Implode

Update for PHP 5.5

PHP 5.5 introduces array_column which is a convenient shortcut to a whole class of array_map usage; it is also applicable here.

$ids = array_column($communications, 'id');
$output = implode(',', $ids);

Original answer

You need to make an array of just ids out of your array of communications. Then the implode would be trivial.

Hint: the function for that is array_map.

Solution:

Assumes PHP 5.3, otherwise you 'd have to write the callback as a string.

$ids = array_map(function($item) { return $item['id']; }, $communications);
$output = implode(',', $ids);

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 Multidimensional Array Keys and Values

You can just use array_keys and array_column:

$dates = implode(',', array_keys($output));
$data_values = implode(',', array_column($output, 'data_values'));

Demo on 3v4l.org

How can I implode() only one column from a multidimensional array?

I like array_column() but if you don't have PHP >= 5.5.0:

$list = implode(',', array_map(function($v) { return $v[1]; }, $data['sales']));

Or with the foreach:

foreach ($data['sales'] as $row) {
$list[] = $row[1];
}
$list = implode(',', $list);

PHP implode multidimensional array - unable to access an array within an array

The following code should do the trick.

$s = '
{"data": [
{
"1": "",
"2": "211",
"3": 0,
"x": [
2661.898,
0
],
"4": 2662.138,
"5": 0,
"y": [
166,
0
]}
]}';

$aData = json_decode($s, true);
$implode = array();

foreach ($aData["data"] as $pos => &$v) {

foreach ($v as $pos2 => $v2) {
if(is_array($v2)){
$aData["data"][$pos][$pos2] = implode(',', $v2);
}
}

$implode[] = implode(',', $v);
}

var_dump($implode);

Produces

array(7) {
[1]=>
string(0) ""
[2]=>
string(3) "211"
[3]=>
int(0)
["x"]=>
string(10) "2661.898,0"
[4]=>
float(2662.138)
[5]=>
int(0)
["y"]=>
string(5) "166,0"
}

Implode and Explode Multi dimensional arrays

You can do this by writing a recursive function:

function multi_implode($array, $glue) {
$ret = '';

foreach ($array as $item) {
if (is_array($item)) {
$ret .= multi_implode($item, $glue) . $glue;
} else {
$ret .= $item . $glue;
}
}

$ret = substr($ret, 0, 0-strlen($glue));

return $ret;
}

As for exploding, this is impossible unless you give some kind of formal structure to the string, in which case you are into the realm of serialisation, for which functions already exist: serialize, json_encode, http_build_query among others.

PHP Multidimensional Array implode?

We can also use array_walk_recursive function for multidimensional arrays:

$your_array = array('Somethings' => 
array(
'Something' =>
array(
0 =>
array(
'@att' =>
array(
'Code' => '0',
),
'Fruit' => 'Apple',
),
1 =>
array(
'@att' =>
array(
'Code' => '3',
),
'Fruit' => 'Banana',
),
2 =>
array(
'@att' =>
array(
'Code' => '1',
),
'Fruit' => 'Pear',
)
)
)
);


$fruits_list="";
$fruits_array = array();
$callback =
function ($value, $key) use (&$fruits_array) {
if($key == "Fruit"){
array_push($fruits_array,$value);

}
};
array_walk_recursive($your_array, $callback);
$fruits_list = implode(',',$fruits_array);
echo $fruits_list;


Related Topics



Leave a reply



Submit