Use implode:
$tags = implode(', ', array('tag1','tag2','tag3','tag4'));
Answer from John Conde on Stack Overflow Top answer 1 of 4
143
Use implode:
$tags = implode(', ', array('tag1','tag2','tag3','tag4'));
2 of 4
31
Yes you can do this by using implode
$string = implode(', ', $tags);
And just so you know, there is an alias of implode, called join
$string = join(', ', $tags);
I tend to use join more than implode as it has a better name (a more self-explanatory name :D )
00:56
Use THIS Method to Convert String to Array in PHP #php #shorts ...
01:46
Convert string with separate commas to an array | PHP | Laravel ...
17:18
PHP - Turn Strings into Arrays with explode() - YouTube
10:35
How to Split a comma delimited string into an array - YouTube
01:35
How to Convert a Comma-Separated String into an Array in PHP - YouTube
05:32
7 Ways To Convert String To Array In PHP - YouTube
Coderwall
coderwall.com › p › zvzwwa › array-to-comma-separated-string-in-php
Array to Comma-Separated String in PHP (Example)
September 14, 2020 - str_getcsv - (PHP 5.3.0 +) Parses a string input for fields in CSV format and returns an array containing the fields read. The snippet of code below implements a new str_putcsv, which expects an array of associative arrays, and returns a CSV formatted string. /** * Convert a multi-dimensional, associative array to CSV data * @param array $data the array of data * @return string CSV text */ function str_putcsv($data) { # Generate CSV data from array $fh = fopen('php://temp', 'rw'); # don't create a file, attempt # to use memory instead # write out the headers fputcsv($fh, array_keys(current($data))); # write out the data foreach ( $data as $row ) { fputcsv($fh, $row); } rewind($fh); $csv = stream_get_contents($fh); fclose($fh); return $csv; }
Agernic
agernic.com › php-tutorial › php-convert-array-to-string-comma-separated.html
PHP convert array to string comma separated, php tutorial
To output an array as a string, where each array value is separated by a comma and a space using implode(), you would do the following example: You can try to execute the following code. ... <!DOCTYPE html> <html> <head> <title>PHP convert array to string comma separated</title> </head> <body> ...
Agernic
agernic.com › php-tutorial › php-array-to-comma-separated-string-with-quotes.html
PHP array to comma separated string with quotes, php tutorial
<!DOCTYPE html> <html> <head> <title>PHP array to comma separated string with quotes</title> </head> <body> <h2>PHP array to comma separated string with quotes - Example</h2> <?php $arr = ['comma','array','quotes','tutorial']; $str = implode(', ', array_map(function($val){return sprintf("'%s'", $val);}, $arr)); echo $str; //'comma','array','quotes','tutorial' ?> </body> </html> Related subjects: PHP convert array to string with commas PHP array to comma separated string with quotes PHP convert array to string comma separated
Thebarton
thebarton.org › fort lauderdale seo › blog › turn a php array or object into a comma separated string
PHP array or object into a comma separated string | Florida Developer
April 20, 2016 - We can also take this a step further and turn the array keys of the associated array into a comma separated array: function to_string($data, $glue=', ', $type='field') { $output = ''; if(!empty($data) && count($data) > 0) { if($type == 'key') { $values = array_keys($data); } else { $values = array_values($data); } $output = join($glue, $values); } return $output; } $myArray = array('blue' => 'Blue', 'yellow' => 'Yellow'); $str = to_string($myArray, $type='keys'); echo $str;
Top answer 1 of 7
107
I would turn it into CSV form, like so:
$string_version = implode(',', $original_array)
You can turn it back by doing:
$destination_array = explode(',', $string_version)
2 of 7
64
I would turn it into a json object, with the added benefit of keeping the keys if you are using an associative array:
$stringRepresentation= json_encode($arr);
php.cn
m.php.cn › home › backend development › php problem › how to convert array to comma separated string in php
How to convert array to comma separated string in php-PHP Problem-php.cn
April 27, 2023 - The above code first defines an empty string $str, and then traverses each value of the $arr array through a foreach loop and adds it one by one. Comma separator. During the traversal process, if it is not the first element, the string $str needs to be added with a comma delimiter, and then the element is added to the string. Finally use echo to output the string $str. ... Above we introduced three methods to convert PHP arrays into comma-separated strings.
Top answer 1 of 3
6
$array = array_column($array, 'app_subject_id'); //Get an array of just the app_subject_id column
$array = implode(',', $array); //creates a string of the id's separated by commas
Output:
1013,1016,1017
2 of 3
3
A one liner will do it:
$appSubjectId = implode(',', array_column($originalArray, 'app_subject_id'));
http://php.net/manual/en/function.implode.php
http://php.net/manual/en/function.array-column.php
Stack Overflow
stackoverflow.com › questions › 40237263 › array-to-comma-separated-string › 40237634
php - Array to comma separated string? - Stack Overflow
Through foreach, all array values are now comma separated. As of now, output will be test 10, test 11,. So, remove extra comma which trail in the end through rtrim(). ... Save this answer.
TutorialsPoint
tutorialspoint.com › article › how-to-create-comma-separated-list-from-an-array-in-php
How to create comma separated list from an array in PHP?
December 26, 2019 - ... <?php $arr = array(100, 200, 300, 400, 500); echo "Original array: "; print_r($arr); echo "\nComma separated list: "; echo implode(', ', $arr); ?> Original array: Array ( [0] => 100 [1] => 200 [2] => 300 [3] => 400 [4] => 500 ) Comma separated ...
Linux Hint
linuxhint.com › php-convert-array-comma-separated-string
Linux Hint – Linux Hint
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
Edureka Community
edureka.co › home › community › categories › web development › php › how to convert an array to a string in php
How to convert an array to a string in PHP | Edureka Community
May 30, 2022 - What can I do to get the array values and store them as a comma-separated string? Array ( [0] => 33160, [ ... > 33138, [24] => 33167, [25] => 33082,)
LinuxHaxor
linuxhaxor.net › code › php-convert-array-comma-separated-string.html
Converting a PHP Array to a Comma Separated String: A Comprehensive Guide - LinuxHaxor
September 19, 2024 - Strings provide compatibility with file formats, user interfaces, and protocols. Converting between the two enables transmitting array data or displaying it textually… [Content truncated for brevity] Take care that values don‘t contain the delimiter character themselves. For example commas:
Tek-Tips
tek-tips.com › home › forums › software › programmers › web development › php
Convert array into comma separated string 1 - PHP
June 8, 2004 - <?php $db = mysql_connect("localhost", ... $my_array=array(); while ($row = mysql_fetch_assoc($result)){ $my_array[]=$row['calls']; } ?> $comma_string=impolde(",",$my_array);...