Rumah > pembangunan bahagian belakang > tutorial php > foreach 追加有关问题,求高手帮忙看看

foreach 追加有关问题,求高手帮忙看看

WBOY
Lepaskan: 2016-06-13 12:19:44
asal
1809 orang telah melayarinya

foreach 追加问题,求高手帮忙看看。

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

foreach ($citycode as $arr => $value) {
$citycode[$arr] = "http://weather.xxx.com/".substr($citycode[$arr], 3,4).".txt";
//$citycode[$arr] .= "http://weather.xxx.com/".$citycode[$arr].".txt";
}

/*
 功能:按行保存为文件
*/

saveFile('api/weather.txt',implode("\r\n",$citycode));
 

?>


我想根据数组保存为换行文件,如下:
http://weather.xxx.com/0101.txt
http://weather.xxx.com/101010100.txt
http://weather.xxx.com/0103.txt
http://weather.xxx.com/101010300.txt

要怎么做?
我现在只能做到:
http://weather.xxx.com/0101.txt
http://weather.xxx.com/0103.txt

怎么追加进去呢?
------解决思路----------------------

<br />foreach ($citycode as $arr => $value) {<br />$citycode[$arr] = "http://weather.xxx.com/".substr($citycode[$arr], 3,4).".txt";<br />//$citycode[$arr] .= "http://weather.xxx.com/".$citycode[$arr].".txt";<br />}<br />
Salin selepas log masuk

你这样明显的用t同样的键值覆盖了之前的值嘛,要不就换个键值如
<br />$citycode[$arr.'1'] = "http://weather.xxx.com/".substr($citycode[$arr], 3,4).".txt";<br />
Salin selepas log masuk

------解决思路----------------------
<br /><?php<br /><br />$citycode = array(<br />	"北京"=>"101010100",<br />	"朝阳"=>"101010300"<br />);<br /><br />$tmp = array();<br /><br />foreach ($citycode as $arr => $value) {<br />	$tmp[] = "http://weather.xxx.com/".substr($value, 3,4).".txt";<br />	$tmp[] = "http://weather.xxx.com/".$value.".txt";<br />}<br /><br />echo implode("\r\n", $tmp);<br /><br />//saveFile('api/weather.txt',implode("\r\n",$citycode));<br />
Salin selepas log masuk


<br />http://weather.xxx.com/0101.txt<br />http://weather.xxx.com/101010100.txt<br />http://weather.xxx.com/0103.txt<br />http://weather.xxx.com/101010300.txt<br />
Salin selepas log masuk


楼主是想这样么?
------解决思路----------------------
$citycode[$arr] .= "http://weather.xxx.com/".$citycode[$arr].".txt";
这里的$citycode[$arr] 已经被修改了
被上一句$citycode[$arr] = "http://weather.xxx.com/".substr($citycode[$arr], 3,4).".txt";

另外:$citycode[$arr] .= "http://weather.xxx.com/".$citycode[$arr].".txt";
使用拼接的话你忘记换行了。

改这样就可以了
<br />$citycode = array(<br />"北京"=>"101010100",<br />"朝阳"=>"101010300"<br />);<br /><br />$result = array();<br /><br />foreach ($citycode as $arr => $value) {<br />    array_push($result,"http://weather.xxx.com/".substr($value, 3,4).".txt");<br />    array_push($result,"http://weather.xxx.com/".$value.".txt");<br />}<br /><br />/*<br /> 功能:按行保存为文件<br />*/<br /><br />saveFile('api/weather.txt',implode("\r\n",$result));<br />
Salin selepas log masuk


http://weather.xxx.com/0101.txt
http://weather.xxx.com/101010100.txt
http://weather.xxx.com/0103.txt
http://weather.xxx.com/101010300.txt

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan