How to use the PHP function array_push () to add array elements. When you use the following example, first demonstrate how to use the PHP function array_push () to add elements to the end of the array: <? * First, we create an array * $ fruitArrayarray (apple, when you useThe following example demonstrates how to use the PHP function array_push () to add elements to the end of an array:
- <? /* First, create an array */
-
- $FruitArray=Array("Apple", "orange", "banana", "Peach", "pear ");
-
- /* Use the array_push () function to add some elements to the end of the original array */
-
- Array_push ($ fruitArray, "grape", "pineapple", "tomato ");
-
- /* Now the keys and values of all elements in the array are displayed on the webpage */
-
- While (list ($ key, $ value) = each ($ fruitArray )){
-
- Echo "$ key: $ value <br> ";
-
- }
-
- ?>
The result is as follows:
0: apple
1: orange
2: banana
3: Peach
4: pear
5: grape
6: pineapple
7: tomato
Now we will 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 () rather than array_push ().
- <?
-
- /* First, create an array */
-
- $FruitArray=Array("Apple", "orange", "banana", "Peach", "pear ");
-
- /* Use the array_unshift () function to add some elements at the beginning of the original array */
-
- Array_unshift ($ fruitArray, "grape", "pineapple", "tomato ");
-
- /* Now the keys and values of all elements in the array are displayed on the webpage */
-
- While (list ($ key, $ value) = each ($ fruitArray )){
-
- Echo "$ key: $ value <br> ";
-
- }
-
- ?>
The result is as follows:
0: grape
1: pineapple
2: tomato
3: apple
4: orange
5: banana
6: Peach
7: pear
The above two sections of code respectively introduce the PHP function array_push () to add elements at the beginning and end of the array.
The following example shows how to use the PHP function array_push () to add elements to the end of an array: <? /* First, create an array */$ fruitArray = array ("apple ","...