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'));
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);
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
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);
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 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
joining 2 dimensional array by columns
Instead of instantiating multiple arrays, you can just work with another two-dimensional array.
Here's a quick pass at an implementation (it could use more error checking, e.g. assumes all of the inner arrays in the input are the same length):
package main
import (
"fmt"
)
func transpose(input [][]int) [][]int {
result := make([][]int, len(input[0]))
for _, row := range(input) {
for j, value := range(row) {
result[j] = append(result[j], value)
}
}
return result
}
func main() {
input := [][]int{{1,2,3,4}, {2,3,4,5}, {3,4,5,6}}
result := transpose(input)
fmt.Println(input)
fmt.Println(result)
}
Output:
[[1 2 3 4] [2 3 4 5] [3 4 5 6]]
[[1 2 3] [2 3 4] [3 4 5] [4 5 6]]
Go Playground
Related Topics
A Way to Determine Browser Width in PHP Without JavaScript
How to Get Last Insert Id in Sqlite
Laravel 5.6 Class App\Http\Controllers\Postcontroller Does Not Exist
How to Add Dynamic Filter Using PHP MySQL Ajax
Get Selected Option in PHP Without Pressing Submit
How to Update Field to Add Value to Existing Value
Regex for Only Allowing Letters, Numbers, Space, Commas, Periods
How to Install Imagick/Imagemagick PHP Extension on Windows 7
Dompdf Not Rendering Images from the Server But Is Rendering from External Source
Php Fatal Error: Uncaught Pdoexception: Could Not Find Driver
How to Set Current Date as Default Value of an Input
On a Function That Gets Settings from a Db I Ran into the Error
Secure Hash and Salt For PHP Passwords
Apache Virtual Host Always Redirecting to /Dashboard
Php Session Lost After Redirect
Transposing Multidimensional Arrays in PHP
Codeigniter: Fatal Error Call to Undefined Function Mysqli_Init()