Home Backend Development PHP Tutorial Learn more about PHP array deletion methods_PHP tutorial

Learn more about PHP array deletion methods_PHP tutorial

Jul 15, 2016 pm 01:27 PM
php introduce element delete study Will array method hour go deep meet question

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">&lt;</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">&lt;</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">&lt;</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">&lt;</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">&lt;</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...
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

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles