Home  >  Article  >  Backend Development  >  关于php中foreach循环的有关问题

关于php中foreach循环的有关问题

WBOY
WBOYOriginal
2016-06-13 12:26:17706browse

关于php中foreach循环的问题

<br />$a ='1=A # 2=B # 3=C # 4=D';<br />$alist = explode(' # ', $a);<br />foreach($alist as $key=>$value){<br />	$arr=explode('=',$value);<br />	$blist[$arr[0]]=$arr[1];<br />	echo "$blist[$key]";<br />}<br />


echo "$blist[$key]"; 只能输出ABC   每次都缺少最后一个   怎么 才能输出 ABCD
------解决思路----------------------
直接 echo $arr[1]; 不就行了

一定要用$blist输出的话应该写成 echo "$blist[$key+1]";  因为你的$blist数组索引是从1开始到4
------解决思路----------------------

引用:
$arr[1]  的确能输出 ABCD 可以解决这个问题 

那么要输出C  怎么书写? $arr[1[3]] ?貌似不对

Quote: 引用:

key是0-3
而$arr[0]是1-4
所以不同,改成这样就可以了。
<br />$a ='1=A # 2=B # 3=C # 4=D';<br />$alist = explode(' # ', $a);<br />foreach($alist as $key=>$value){<br />    $arr=explode('=',$value);<br />    $blist[$arr[0]-1]=$arr[1];<br />    echo "$blist[$key]";<br />}<br />



$arr[1]  的确能输出 ABCD 可以解决这个问题 

那么要输出C  怎么书写? $arr[1[3]] ?貌似不对


echo $blist[2];
Statement:
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