Php echo array as string
Use PHP implode to convert your Array to a string that you can echo. Using echo on an array will just display the data type. Show
If you want to separate the errors with a delimiter other than a space, just replace the space in the first parameter:
For example, if your errors array contained three values:
then your
string, if you used the ::, would look like this when you run
See the reference link I included for another example. (PHP 4, PHP 5, PHP 7, PHP 8) implode — Join array elements with a string Descriptionimplode(string Alternative signature (not supported with named arguments): implode(array
Legacy signature (deprecated as of PHP 7.4.0, removed as of PHP 8.0.0): implode(array Parametersseparator Optional. Defaults to an empty string. array The array of strings to implode. Return ValuesReturns a string containing a string representation of all the array elements in the same order, with the separator string between each element. Changelog
ExamplesExample #1 implode() example
= ['lastname', 'email', 'phone']; Notes
See Also
houston_roadrunner at yahoo dot com ¶ 13 years ago
"a1 is: '".implode("','",$a1)."' biziclop ¶ 1 year ago
wrap_implode(['path','to','file.php'], '/'); ASchmidt at Anamera dot net ¶ 3 years ago
implode( ',', $a ),'/', implode( ',', $b ); omar dot ajoue at kekanto dot com ¶ 9 years ago
= array('a', 'b', 'c'); Honk der Hase ¶ 2 years ago
mapped_implode(', ', $arr, ' is ');// output: x is 5, y is 7, z is 99, hello is World, 7 is Foo?>
Felix Rauch ¶ 6 years ago
__construct($title) alexey dot klimko at gmail dot com ¶ 11 years ago
Anonymous ¶ 9 years ago
masterandujar ¶ 10 years ago
Anonymous ¶ 7 years ago
Rafael Pereira ¶ 2 years ago
admin at lanlink dot net dot au ¶ 5 years ago
info at ensostudio dot ru ¶ 2 years ago
a' => 1, 'b' => 2]; info AT sinistercircuits DOT com ¶ 10 months ago
How do I turn an array into a string in PHP?There are two ways to convert an array into a string in PHP.. Using implode() function.. Using json_encode() function.. How do I echo an array in PHP?Echo or Print an Array in PHP. Use foreach Loop to Echo or Print an Array in PHP.. Use print_r() Function to Echo or Print an Array in PHP.. Use var_dump() Function to Echo or Print an Array in PHP.. How do you make an array into a string?Below are the various methods to convert an Array to String in Java:. Arrays. toString() method: Arrays. toString() method is used to return a string representation of the contents of the specified array. ... . StringBuilder append(char[]): The java. lang. StringBuilder.. How we can convert a multidimensional array to string without any loop in PHP?Multidimensional PHP Array to String
Maybe in case you need to convert a multidimensional array into a string, you may want to use the print_r() function. This is also called “array with keys”. By adding “true” as its second parameter, all the contents of the array will be cast into the string.
|