PHP,添加内含换行符 [英] PHP, add a newline with implode

查看:277
本文介绍了PHP,添加内含换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的foreach语句中添加一个换行符\n.

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

我的代码:

$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'],

['Country', 'number'],

['Country','number']

['Country', 'number']

但是我得到了:

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

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

为什么我的\ n不引起换行符?

Why does my \n not cause a newline?

推荐答案

我怀疑是因为您正在将数据回显到浏览器,并且没有按预期显示换行符.如果将爆破包装在<pre>标记中,则可以看到它正常工作.

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>";

输出:

this,
is,
an,
array

这篇关于PHP,添加内含换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆