Home > Backend Development > PHP Tutorial > foreach 输出遇到的问题,求各位老师解决

foreach 输出遇到的问题,求各位老师解决

WBOY
Release: 2016-06-23 13:36:39
Original
974 people have browsed it

$citycode = array("北京"=>"101010100","海淀"=>"101010200","朝阳"=>"101010300");

foreach ($citycode as $arr) {

echo $arr.'\\n';
}
saveFile('file.txt',$arr);

我希望file.txt里的内容为:

101010100,101010200,101010300


这个$arr要怎么写?


回复讨论(解决方案)

<?php$citycode = array("北京"=>"101010100","海淀"=>"101010200","朝阳"=>"101010300");foreach ($citycode as $arr => $value) {    $citycode[$arr] .= ',';}// saveFile('file.txt',$arr);file_put_contents('file.txt', $citycode);?>
Copy after login

这样岂不是更简单

$arr = implode(',', $citycode);saveFile('file.txt',$arr);
Copy after login
Copy after login

这样岂不是更简单

$arr = implode(',', $citycode);saveFile('file.txt',$arr);
Copy after login
Copy after login



居然忘了这个函数。。。

$citycode = array("北京"=>"101010100","海淀"=>"101010200","朝阳"=>"101010300");saveFile('file.txt',implode(',',$citycode));
Copy after login

其实我就是在想jordan102的,我写JS写多了,脑子尽然一天没转过弯来。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template