Home >Backend Development >PHP Tutorial >How to use the PHP function array_push() to add array elements_PHP tutorial
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>
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>
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.