Rumah >pembangunan bahagian belakang >tutorial php >深入学习PHP数组删除元素方法_PHP教程
学习PHP时,你可能会遇到PHP数组删除问题,这里将介绍PHP数组删除问题的解决方法,在这里拿出来和大家分享一下。学过C语言的人可能都知道,C语言对字符串的处理功能很强,PHP是用C写成的,自然不例外的继承了C的处理字符串的优势。不过毕竟PHP是一门新的语言,与C语言还是有区别的,自然不能保证与C是完全相同的。所以有些功能只有经过测试才能知道。 有时候需要对一个字符串的每个字符进行处理,一般的做法可能是:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">str</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"something"</font></span><span>; </span></span></li> <li class=""> <span>for($</span><span class="attribute"><font color="#ff0000">i</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>; $i</span><strong><font color="#006699"><span class="tag"><span class="tag-name">strlen</span></span></font></strong><span>($str); $i++) </span> </li> <li class="alt"><span>{ </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">ch</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">substr</font></span><span>($str, $i, 1); </span> </li> <li class="alt"><span>//处理$ch </span></li> <li class=""><span>} </span></li> </ol>
这样是可以,但是有没有更优美一点的办法呢?有,就是把它看成一个数组,C语言就是这样处理的。
我们把上面的例子,改成用字符串数组的处理方式:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">str</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"something"</font></span><span>; </span></span></li> <li class=""> <span>for($</span><span class="attribute"><font color="#ff0000">i</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>; $i</span><strong><font color="#006699"><span class="tag"><span class="tag-name">strlen</span></span></font></strong><span>($str); $i++) </span> </li> <li class="alt"><span>{ </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">ch</font></span><span>=$str[$i]; </span> </li> <li class="alt"><span>//处理$ch </span></li> <li class=""><span>} </span></li> </ol>
这样是不是好多了。
PHP数组删除元素
定义了一个数组,有时想PHP数组删除其中几项怎么办?我在www.phpbuilder.com中看到了一个答案,就是使用unset()函数。还是让我们做一个测试吧。
<ol class="dp-xml"> <li class="alt"><span><span>$a[]="a1"; </span></span></li> <li class=""><span>$a[]="a2"; </span></li> <li class="alt"><span>$a[]="a3"; </span></li> <li class=""> <span>for($</span><span class="attribute"><font color="#ff0000">i</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>; $i</span><strong><font color="#006699"><span class="tag"><span class="tag-name">sizeof</span></span></font></strong><span>($a); $i++) </span> </li> <li class="alt"><span>{ </span></li> <li class=""><span>echo $a[$i] . " </span></li> <li class="alt"><span>"; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>unset($a[0]); </span></li> <li class=""> <span>for($</span><span class="attribute"><font color="#ff0000">i</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>; $i</span><strong><font color="#006699"><span class="tag"><span class="tag-name">sizeof</span></span></font></strong><span>($a); $i++) </span> </li> <li class="alt"><span>{ </span></li> <li class=""><span>echo $a[$i] . " </span></li> <li class="alt"><span>"; </span></li> <li class=""><span>} </span></li> </ol>
<ol class="dp-xml"> <li class="alt"><span><span>$a[]="a1"; </span></span></li> <li class=""><span>$a[]="a2"; </span></li> <li class="alt"><span>$a[]="a3"; </span></li> <li class=""> <span>for($</span><span class="attribute"><font color="#ff0000">i</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>; $i</span><strong><font color="#006699"><span class="tag"><span class="tag-name">sizeof</span></span></font></strong><span>($a); $i++) </span> </li> <li class="alt"><span>{ </span></li> <li class=""><span>echo $a[$i] . " </span></li> <li class="alt"><span>"; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>unset($a[0]); </span></li> <li class=""><span>reset($a); //使数组指针回到第1个元素 </span></li> <li class="alt"><span>while(list($c, $d)=each($a)) </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>echo $d . " </span></li> <li class=""><span>"; //$c为数组下标 </span></li> <li class="alt"><span>} </span></li> </ol>
这是一个通用的显示数组的方法,可以不用考虑数组的下标了。