When you useThe following example first demonstrates how to use the PHP function Array_push () to add elements to the end of an array:
- ? /* First we create an array */
- $ Fruitarray = Array ("Apple", "orange", "banana", "Peach", "pear");
- /* Add some elements to the end of the original array using the Array_push () function */
- Array_push ($fruitArray, "Grape", "pineapple", "tomato");
- /* Now display the keys and values (value) for all elements of the array on the Web page */
- while (list ($key, $value) = each ($fruitArray)) {
- echo "$key: $value
";
- }
- ? >
The results appear as follows:
0:apple
1:orange
2:banana
3:peach
4:pear
5:grape
6:pineapple
7:tomato
Now demonstrate how to use PHP function Array_push () to add elements from the beginning of the array. The following program code is almost identical to the previous one, except that the function used here is array_unshift () instead of Array_push ().
- ?
- /* First we create an array */
- $ Fruitarray = Array ("Apple", "orange", "banana", "Peach", "pear");
- /* Add some elements to the beginning of the original array using the Array_unshift () function */
- Array_unshift ($fruitArray, "Grape", "pineapple", "tomato");
- /* Now display the keys and values (value) for all elements of the array on the Web page */
- while (list ($key, $value) = each ($fruitArray)) {
- echo "$key: $value
";
- }
- ? >
The results appear as follows:
0:grape
1:pineapple
2:tomato
3:apple
4:orange
5:banana
6:peach
7:pear
The above two pieces of code describes the PHP function Array_push () in the array at the tail of the method of adding elements.
http://www.bkjia.com/PHPjc/446378.html www.bkjia.com true http://www.bkjia.com/PHPjc/446378.html techarticle when you use the following example first demonstrate how to use PHP function Array_push () to add elements at the end of the array: