Usage and definition of the array_push () function of the php array operation function _ PHP Tutorial

Source: Internet
Author: User
Usage and definition of array_push () function for php array operation functions. A simple array_push () function is a commonly used function for Array Operations. if you need it, refer to (PHP4, PHP5) array_push uses a simple array_push () function for one or more units. this is a commonly used function for Array Operations. if you need it, please refer to it.

(PHP 4, PHP 5)

Array_push-push one or more units to the end of the array (into the stack)

Report a bug description
Int array_push (array & $ array, mixed $ var [, mixed $...])
Array_push () treats array as a stack and pushes the passed variables to the end of array. The length of array increases according to the number of variables in the stack. The effect is the same as the following:

The code is as follows:
$ Array [] = $ var;
?>

Repeat the preceding actions for each var.

Returns the total number of new units in the 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)

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.

See array_pop (), array_shift (), and array_unshift ().


If you want to preserve the keys in the array, use the following:

The code is as follows:

Function array_pshift (& $ array ){
$ Keys = array_keys ($ array );
$ Key = array_shift ($ keys );
$ Element = $ array [$ key];
Unset ($ array [$ key]);
Return $ element;
}
?>

Evaluate () function usage. this is a commonly used function for Array Operations. For more information, see (PHP 4, PHP 5) array_push will put one or more units...

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.