Use of PHP array processing functions Array_push (i)

Source: Internet
Author: User

Using PHP to do data processing involves a large number of array operations, here I write down the use of various array processing functions, good memory is not as bad as written!!

First, the use of Array_push: pressing one or more cells into the end of the array (into the stack)

  description : int array_push (array & $array, mixed $var [, mixed $ ...] parameter Description : Array is the input $var the value to be pressed into

The mixed here shows that the parameter can accept a number of different (but not necessarily all) types.

Array_push () treats the array as a stack and presses the incoming variable into the end of the array. The length of the array will increase depending on the number of variables that are in the stack. The effect is the same as the following:

1 <? PHP 2 $array $var ; 3 ?>   
View Code

and repeat the above actions for each VAR, which is equivalent to $array[] multiple assignment operations are performed.

return Value: returns the number of array elements after processing

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

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

The main purpose of learning these functions is to be able to use in the process of developing projects in peacetime, here is a small example of the use of Array_push:

1 $user=Array( 20 =Array( 3' id ' = 1,4' Name ' = ' Zhang San ',5' Email ' = ' [email protected] ',6),71 =Array( 8' id ' = 2,9' Name ' = ' John Doe ',Ten' Email ' = ' [email protected] ', One), A9 =Array(  -' id ' = 5, -' Name ' = ' Harry ', the' Email ' = ' [email protected] ', -   ) -);
View Code

Assuming that the above two-dimensional array is data fetched from the database, I want to get a collection of name columns, like this:

, we can use a combination of foreach and Array_push to get it.

1 $ids Array  2foreach ($useras$key$value) {3  array_push($ids,$value[' name ']); 4 }   
View Code

Perform the print function print_t (), as shown above! Of course you can also get an array of any columns (ID, email, etc.).

I will list more convenient ways to achieve the same purpose in future articles!!

  

Use of PHP array processing functions Array_push (i)

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.