PHP, add a newline with implode [duplicate]
Asked Answered
P

4

25

I'm trying to add a newline \n, in my foreach statement with implode.

My code:

$ga->requestReportData($profileId,array('country'),array('visits')); 
$array = array();
foreach($ga->getResults() as $result){ 
    $array[] = "['".$result->getcountry()."', ".$result->getVisits()."]"; 
} 
echo implode(",\n", $array);

I only get a comma and a space between my results. I want a comma and a newline.

I am trying to get something like this:

['Country', 'number'],

['Country', 'number'],

['Country', 'number']

However I I get this:

['Country', 'number'], ['Country', 'number'], ['Country', 'number']

Why does my \n not cause a newline?

Phrixus answered 5/2, 2014 at 17:25 Comment(7)
Can you show your array and the result you are trying to get?Tetreault
make sure you are printing your result in a <pre></pre> tag or you will only see a spaceForwards
try implode( ",\n",$array); see: bd1.php.net/function.implodeAiden
Not working in this way ...Phrixus
well, first of all, post your real code. second: explain what's the problem. "not working" is ridiculously vague. (and check the answers...)Snigger
You shouldn't be manually generating json strings.Dopester
How to concatenate the echo of an array as expressed through a foreach loop?Dopester
K
31

I suspect it is because you are echoing the data to the browser and it's not showing the line break as you expect. If you wrap your implode in the the <pre> tags, you can see it is working properly.

Additionally, your arguments are backwards on your implode function, according to current documentation. However, for historical reasons, parameters can be in either order.

$array = array('this','is','an','array');
echo "<pre>".implode(",\n",$array)."</pre>";

Output:

this,
is,
an,
array
Kathline answered 5/2, 2014 at 17:30 Comment(2)
Have a look at the docs: implode() can, for historical reasons, accept its parameters in either order. For consistency with explode(), however, it may be less confusing to use the documented order of arguments.Bassett
Is there any option without <pre> tagSejm
H
29

For cross-platform-compatibility use PHP_EOL instead of \n.

Using the example from the accepted answer above:

$array = array('this','is','another','way');
echo "<pre>".implode(PHP_EOL, $array)."</pre>";

If you're writing directly to HTML (it wouldn't work on files) there is an option of using <br> like this:

$array = array('this','is','another','way');
echo "<p>".implode(<br>, $array)."</p>";

Both output:

this, 
is, 
another, 
way
Heterotypic answered 30/1, 2018 at 15:43 Comment(4)
Correct answer. It also addressed the portability issues.Showcase
Using PHP_EOL worked for me when adding to a text area inputHillock
PHP_EOL worked (In laravel). If you want ',' also, then you can write this way: implode(','.PHP_EOL, $array).Rivkarivkah
This should be marked as the correct answer, '\n' will not work on windows and if you accidantally write '\n' instead of "\n", it won't work too.Queeniequeenly
P
4

This can also work

$array = array('one','two','three','four');
echo implode("<br>", $array);

Output:

one
two
three
four
Physiologist answered 27/3, 2018 at 6:48 Comment(0)
B
-1

Many others claim you use the wrong order, that's only partial right, because the docs only recommend this, but you don't have to:

implode() can, for historical reasons, accept its parameters in either order. For consistency with explode(), however, it may be less confusing to use the documented order of arguments.

I think your problem is caused by how browsers interpret HTML. They don't care about newlines, they're like a normal space for them.

To show these linebreaks, you can use <pre><?php echo implode($glue, $array); ?></pre>. You could also use nl2br(implode(..)) or nl2br(implode(..), true) if you're writing XHTML.

Bassett answered 5/2, 2014 at 17:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.