How to use PHP function Array_push () to implement an increase in array elements _php tutorial

Source: Internet
Author: User
Tags pear
When you useThe following example first demonstrates how to use the PHP function Array_push () to add elements to the end of an array:

 
 
  1. ? /* First we create an array */
  2. $ Fruitarray = Array ("Apple", "orange", "banana", "Peach", "pear");
  3. /* Add some elements to the end of the original array using the Array_push () function */
  4. Array_push ($fruitArray, "Grape", "pineapple", "tomato");
  5. /* Now display the keys and values (value) for all elements of the array on the Web page */
  6. while (list ($key, $value) = each ($fruitArray)) {
  7. echo "$key: $value
    ";
  8. }
  9. ? >

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

 
 
  1. ?
  2. /* First we create an array */
  3. $ Fruitarray = Array ("Apple", "orange", "banana", "Peach", "pear");
  4. /* Add some elements to the beginning of the original array using the Array_unshift () function */
  5. Array_unshift ($fruitArray, "Grape", "pineapple", "tomato");
  6. /* Now display the keys and values (value) for all elements of the array on the Web page */
  7. while (list ($key, $value) = each ($fruitArray)) {
  8. echo "$key: $value
    ";
  9. }
  10. ? >

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:

  • 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.