Add one or more elements (in the stack) to the end of the array_push () array of the php array function sequence, and return the new length. _ PHP Tutorial

Source: Internet
Author: User
Add one or more elements (in the stack) to the end of the array_push () array of the php array function sequence, and return the new length .. Array_push () defines and uses the array_push () function to add one or more elements (in the stack) to the end of the array of the first parameter, and then return the length of the new array. This function is equivalent to calling array_push () multiple times for definition and usage.
The array_push () function adds one or more elements (in the stack) to the end of the array of the first parameter, and returns the length of the new array.

This function is equivalent to calling $ array [] = $ value multiple times.

Syntax
Array_push (array, value1, value2...) parameter description
Array is required. Specifies an array.
Value1 is required. Specifies the value to be added.
Value2 is optional. Specifies the value to be added.

Tips and comments
Note: Even if the array contains a string key name, the element you add is always a numeric key. (See example 2)

Note: If array_push () is used to add a unit to the array, it is better to use $ array [] =, because there is no additional burden to call the function.

Note: If the first parameter is not an array, array_push () will issue a warning. This is different from the behavior of $ var []. The latter creates an array.

Example 1

The code is as follows:


$ A = array ("Dog", "Cat ");
Array_push ($ a, "Horse", "Bird ");
Print_r ($ );
?>


Output:

Array ([0] => Dog [1] => Cat [2] => Horse [3] => Bird) Example 2
Arrays with string keys:

The code is as follows:


$ A = array ("a" => "Dog", "B" => "Cat ");
Array_push ($ a, "Horse", "Bird ");
Print_r ($ );
?>


Output:

Array ([a] => Dog [B] => Cat [0] => Horse [1] => Bird)

The http://www.bkjia.com/PHPjc/324618.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/324618.htmlTechArticlearray_push () defines and uses the array_push () function to add one or more elements (in the stack) to the end of the array of the first parameter, and then returns the length of the new array. This function is called multiple times...

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.