How do I create a comma-separated list from an array in PHP?
You want to use implode for this.
ie:$commaList = implode(', ', $fruit);
There is a way to append commas without having a trailing one. You'd want to do this if you have to do some other manipulation at the same time. For example, maybe you want to quote each fruit and then separate them all by commas:
$prefix = $fruitList = '';
foreach ($fruits as $fruit)
{
$fruitList .= $prefix . '"' . $fruit . '"';
$prefix = ', ';
}
Also, if you just do it the "normal" way of appending a comma after each item (like it sounds you were doing before), and you need to trim the last one off, just do $list = rtrim($list, ', ')
. I see a lot of people unnecessarily mucking around with substr
in this situation.
Generate Array from a comma-separated list - PHP
Check out explode
: $thePostIdArray = explode(', ', $var);
Create Comma separated Array in PHP?
Try this
<?php
echo "<pre>";
$arr = array('fruit1'=>'banana','fruit2'=>'apple','fruit3'=>'grapes','fruit4'=>'orange');
$new = array_values($arr);
print_r($new);
For more info about array_values
please read http://php.net/manual/en/function.array-values.php
Output
Array
(
[0] => banana
[1] => apple
[2] => grapes
[3] => orange
)
PHP Array in to comma separated list
You're just about there.
$tokens = array();
foreach($result as $device){
$tokens[] = $device['dToken'];
}
echo implode(',', $tokens);
Split a comma-delimited string into an array?
Try explode:
$myString = "9,admin@example.com,8";
$myArray = explode(',', $myString);
print_r($myArray);
Output :
Array
(
[0] => 9
[1] => admin@example.com
[2] => 8
)
Comma separated list from array
so, revising my answer to actually address your question, you could do it with nested foreach loops like this:
<?php
$a1 = array(
'name' => array( 0 => 'Some message to display1'),
'test' => array( 0 => 'Some message to display2'),
'kudos' => array( 0 => 'Some message to display3'),
);
$final = "";
foreach($a1 as $innerarray){
foreach($innerarray as $message){
$final .= $message.", ";
}
}
echo substr($final,0,-2);
?>
php, how to convert an array containing stdClass Object into comma separated list
Except for the comma separated list, what you have should work in theory, but the code doesn't at all match your array. You state $object
not $array
and the property is id
not nid
.
However, just extract the id
column and implode. If you have PHP 7:
$list = implode(', ', array_column($output, 'id'));
For older versions:
$list = implode(', ', array_map(function($v) { return $v->id; }, $output));
PHP: How to make an array by a comma separated string?
You can use explode() like this:
$string = "37,40,42,46,49,54,56,57";
$array = explode("," , $string);
How to convert items in array to a comma separated string in PHP?
Use implode:
$tags = implode(', ', array('tag1','tag2','tag3','tag4'));
PHP build an array from a comma separated list
<?php
$values = '0,33,0,0,138,1231';
$array = explode(',', $values);
var_dump($array);
?>
The output:
array(6) { [0]=> string(2) ""0" [1]=> string(2) "33" [2]=> string(1) "0" [3]=> string(1) "0" [4]=> string(3) "138" [5]=> string(4) "1231" }
More info here
Related Topics
What Are the Best Practices For Avoiding Xss Attacks in a PHP Site
How to Compare Two Dates in PHP
Use PHP to Create, Edit and Delete Crontab Jobs
How to Remove  from the Beginning of a File
How to Get Multiple Selected Values of Select Box in PHP
How to Rename Uploaded File Before Saving It into a Directory
Session Variables Not Working PHP
How to Check If a Url Exists Via PHP
How to Zip a Whole Folder Using PHP
How to Log Errors and Warnings into a File
PHP Elvis Operator VS Null Coalescing Operator
How to Create Friendly Urls With .Htaccess