How to use the PHP function array_push () to add array elements _ PHP Tutorial

Source: Internet
Author: User
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:

 
 
  1. <? /* First, create an array */
  2. $FruitArray=Array("Apple", "orange", "banana", "Peach", "pear ");
  3. /* Use the array_push () function to add some elements to the end of the original array */
  4. Array_push ($ fruitArray, "grape", "pineapple", "tomato ");
  5. /* Now the keys and values of all elements in the array are displayed on the webpage */
  6. While (list ($ key, $ value) = each ($ fruitArray )){
  7. Echo "$ key: $ value <br> ";
  8. }
  9. ?>

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 ().

 
 
  1. <?
  2. /* First, create an array */
  3. $FruitArray=Array("Apple", "orange", "banana", "Peach", "pear ");
  4. /* Use the array_unshift () function to add some elements at the beginning of the original array */
  5. Array_unshift ($ fruitArray, "grape", "pineapple", "tomato ");
  6. /* Now the keys and values of all elements in the array are displayed on the webpage */
  7. While (list ($ key, $ value) = each ($ fruitArray )){
  8. Echo "$ key: $ value <br> ";
  9. }
  10. ?>

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 ","...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.