I have the following array
$appArray=array('a', 'b', 'c');
I want to produce output such as 'a\nb\nc\n'
. The trouble is that when I use
implode('\n', $appArray)
I get 'a\\nb\\nc\\n'
( note the extra backslash).
Any idea how to fix this?
The implode() is a builtin function in PHP and is used to join the elements of an array. implode() is an alias for PHP | join() function and works exactly same as that of join() function. If we have an array of elements, we can use the implode() function to join them all to form one string.
As the name suggests Implode/implode() method joins array elements with a string segment that works as a glue and similarly Explode/explode() method does the exact opposite i.e. given a string and a delimiter it creates an array of strings separating with the help of the delimiter.
The implode method will help to generate string using glue with given argument key. I will give you simple example of implode() colletion in laravel. so you can easily use it with your laravel 5, laravel 6, laravel 7, laravel 8 and laravel 9 application.08-Oct-2020.
Are you sure you're not intending: implode("\n", $appArray)
? Newline characters aren't actually treated as newline characters when encapsulated in 'single quotes'.
Use PHP_EOL (end of line) :
implode(PHP_EOL, $array);
Actually, in single quotes \n means \n (literally), not carriage return. Try using double quotes in implode ().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With