PHP Array function sequence Array_push () the tail of the array adds one or more elements (into the stack), returning the new length. _php Tips

Source: Internet
Author: User
Array_push () Definition and usage
The Array_push () function adds one or more elements (into the stack) to the end of the array of the first argument, and then returns the length of the new array.

This function equals multiple calls to the $array [] = $value.

Grammar
Array_push (Array,value1,value2 ...) Parameter description
Array required. Specify an array.
Value1 required. Specify the value to add.
Value2 Optional. Specify the value to add.

Tips and comments
Note: Even if there is a string key in the array, the element you are adding is always a numeric key. (see Example 2)

Note: If you use Array_push () to add a cell to an array, you might as well use $array [] =, because there is no additional burden to invoke the function.

Note: If the first argument is not an array, Array_push () will issue a warning. This differs from the behavior of $var [], which creates a new array.

Example 1
Copy Code code as follows:

<?php
$a =array ("Dog", "Cat");
Array_push ($a, "horse", "Bird");
Print_r ($a);
?>

Output:

Array ([0] => Dog [1] => Cat [2] => horse [3] => Bird) Example 2
An array with a string key:
Copy Code code as follows:

<?php
$a =array ("a" => "Dog", "B" => "Cat");
Array_push ($a, "horse", "Bird");
Print_r ($a);
?>

Output:

Array ([a] => Dog [b] => Cat [0] => horse [1] => Bird)

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.