How to Print array in one line PHP
✔ Recommended Answer
You have the default
print_r()
output. There is no option for formatting this, but you can transform the output as needed.Convert the array to JSON and do some replacements. You can go further and replace the quotes and spaces after comma.
$array = [10 => 11.22, 20 => 22.33, 30 => 33.44];echo str_replace([':', '{', '}'], ['=>', '[', ']'], json_encode($array));
["10"=>11.22,"20"=>22.33,"30"=>33.44]
- But that's not a good choice. Better is to reconstruct the output by the transforms of your needs.
$format = function ($key, $value) { return "$key => $value";};echo '[', join(', ', array_map($format, array_keys($array), $array)), ']';
[10 => 11.22, 20 => 22.33, 30 => 33.44]
Source: stackoverflow.com
Answered By: Markus Zeller
The implode() function joins the elements of the $array array with a comma and a space as the separator, and then outputs the resulting string using the echo statement.
Comments
Post a Comment