When learning PHP, you may encounter the problem of deleting PHP arrays. Here we will introduce the solution to the problem of deleting PHP arrays, and share it with you here. Anyone who has studied C language may know that C language has strong string processing capabilities. PHP is written in C, so it naturally inherits C's advantages in string processing. But after all, PHP is a new language, and it is still different from the C language. Naturally, there is no guarantee that it is exactly the same as C. So some features can only be known through testing. Sometimes it is necessary to process each character of a string. The general approach may be:
<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><span class="tag-name">strlen</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>
This is okay, but is there a more elegant way? Yes, just treat it as an array, which is how C language handles it.
Let’s change the above example to use a string array:
<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><span class="tag-name">strlen</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>
Isn’t this much better.
Delete elements from PHP array
defines an array, what should I do if I want to delete several elements from the PHP array? I saw an answer in www.phpbuilder.com, which is to use the unset() function. Let's do a test. What does
<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><span class="tag-name">sizeof</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><span class="tag-name">sizeof</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><span class="tag-name">sizeof</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>
This is a general method of displaying an array, and you don’t need to consider the subscript of the array.