Learn more about PHP array deletion methods_PHP tutorial

WBOY
Release: 2016-07-15 13:27:25
Original
676 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

mean? First generate an array $a with three elements, display it, then delete the first one (subscript 0), and then display it. The result should be that there are two elements left in the array. But that’s not right! The answer is different from what we thought. Is it because unset() is not easy to use? If you think about it carefully, it turns out that unset($a[0]) deletes the first element, but when outputting, we still start from $i=0, which is of course wrong. PHP will not automatically adjust the subscript. So we have to use other methods to deal with it:
<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>
Copy after login

This is a general method of displaying an array, and you don’t need to consider the subscript of the array.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446534.htmlTechArticleWhen learning PHP, you may encounter the problem of deleting PHP arrays. Here we will introduce the solution to the problem of deleting PHP arrays. , take it out here and share it with everyone. Anyone who has studied C language may know...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!