Home > Backend Development > PHP Tutorial > How to use the PHP function array_push() to add array elements_PHP tutorial

How to use the PHP function array_push() to add array elements_PHP tutorial

WBOY
Release: 2016-07-15 13:29:09
Original
1224 people have browsed it

When you use The following example first demonstrates how to use the PHP function array_push() to add elements to the end of the array:

<ol class="dp-xml">
<li class="alt"><span><span><? /* 首先我们建立一个数组 */   </span></span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">fruitArray</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>("apple", "orange", "banana", "Peach", "pear");    </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>/* 使用array_push()函数在原有数组尾端新增一些元素 */   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>array_push($fruitArray, "grape", "pineapple", "tomato");    </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>/* 现在把该数组的所有元素的键(key)与值(value)都显示在网页上 */   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>while (list($key,$value) = each($fruitArray)) {   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>echo "$key : $value<br>";   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>}    </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>?>     </span></li>
</ol>
Copy after login

The results are as follows:

0 : apple

1 : orange

2 : banana

3 : Peach

4 : pear

5: grape

6: pineapple

7: tomato

Now let’s demonstrate how to use the PHP function array_push() to add some elements from the beginning of the array. The following program code is almost identical to the previous example, the only difference is that the function used here is array_unshift() instead of array_push().

<ol class="dp-xml">
<li class="alt"><span><span><?   </span></span></li>
<li class=""><span> </span></li>
<li class="alt"><span>/* 首先我们建立一个数组 */   </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">fruitArray</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>("apple", "orange", "banana", "Peach", "pear");    </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>/* 使用array_unshift()函数在原有数组开头新增一些元素 */   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>array_unshift($fruitArray, "grape", "pineapple", "tomato");    </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>/* 现在把该数组的所有元素的键(key)与值(value)都显示在网页上 */   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>while (list($key,$value) = each($fruitArray)) {   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>echo "$key : $value<br>";   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>}    </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>?>   </span></li>
</ol>
Copy after login

displays the results as follows:

0 : grape

1 : pineapple

2 : tomato

3 : apple

4 : orange

5 : banana

6 : Peach

7 : pear

The above two pieces of code introduce PHP functions respectively. array_push() is a method to add elements at the beginning and end of an array.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446378.htmlTechArticleWhen you use the following example, first demonstrate how to use the PHP function array_push() to add elements at the end of the array : <?/*First we create an array*/ $ fruitArray = array ("apple","...
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