[php] Return single column from a multi-dimensional array

I'm a novice at PHP and I need a quick solution to the following problem but can't seem to come up with one:

I have a multi-dimensional array like so

Array
(
    [0] => Array
        (
            [blogTags_id] => 1
            [tag_name] => google
            [inserted_on] => 2013-05-22 09:51:34
            [inserted_by] => 2
        )

    [1] => Array
        (
            [blogTags_id] => 2
            [tag_name] => technology
            [inserted_on] => 2013-05-22 09:51:34
            [inserted_by] => 2
        )
)

I want to use the implode() to somehow return a comma-separated string containing values of tag_name key like so.

google, technology

Is it possible to achieve this effect with the said function? If not then please suggest an alternate solution.

This question is related to php arrays implode array-column

The answer is


array_map is a call back function, where you can play with the passed array. this should work.

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

In this situation implode($array,','); will works, becasue you want the values only. In PHP 5.6 working for me.

If you want to implode the keys and the values in one like :
blogTags_id: 1
tag_name: google

$toImplode=array();
foreach($array as $key => $value) {
$toImplode[]= "$key: $value".'<br>';
}
$imploded=implode('',$toImplode);

Sorry, I understand wrong, becasue the title "Implode data from a multi-dimensional array". Well, my answer still answer it somehow, may help someone, so will not delete it.


Although this question is related to string conversion, I stumbled upon this while wanting an easy way to write arrays to my log files. If you just want the info, and don't care about the exact cleanliness of a string you might consider:

json_encode($array)

join(',', array_map(function (array $tag) { return $tag['tag_name']; }, $array))

If you want "tag_name" with associated "blogTags_id" use: (PHP > 5.5)

$blogDatas = array_column($your_multi_dim_array, 'tag_name', 'blogTags_id');
echo implode(', ', array_map(function ($k, $v) { return "$k: $v"; }, array_keys($blogDatas), array_values($blogDatas)));

very simple go for this

$str;
foreach ($arrays as $arr) {
$str .= $arr["tag_name"] . ",";
}
$str = trim($str, ',');//removes the final comma 

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to implode

Return single column from a multi-dimensional array How to implode array with key and value without foreach in PHP php implode (101) with quotes Implode an array with JavaScript? Fastest way to implode an associative array with keys

Examples related to array-column

Return single column from a multi-dimensional array Check if a specific value exists at a specific key in any subarray of a multidimensional array