Common Array processing methods in PHP instance analysis

Source: Internet
Author: User
Sort out several methods commonly used in php to process arrays, so that you can learn more. you can run them one by one and get familiar with the use of php arrays. array_shift () removes the first unit of the array and returns the result. This is convenient for the first unit of the array to be processed independently.

The code is as follows:

$ Tmparray = array ("1", "2", "3", "4 ");
$ Tmparray = array_shift ($ tmparray );
Print_r ($ tmparray );

2. array_chunk () splits an array into multiple arrays, and the following parameters control the number of array units.

The code is as follows:

$ Tmparray = array ('A', 'B', 'C', 'D', 'E ');
Print_r (array_chunk ($ tmparray, 2 ));

This function is useful in some loops. for example, I want to put the obtained data into N rows and M columns. If we directly loop, we certainly won't be able to implement it. of course, nested loops can be implemented, but it is too troublesome. if we use the array_chunk () function to process and then return a new array, it is very convenient.
3. array_push () pushes the input value to the end of the array.

The code is as follows:

$ Tmparray = array ("a", "B ");
Array_push ($ tmparray, "c", "d ");
Print_r ($ tmparray); // Array ([0] => a [1] => B [2] => c [3] => d)

4. array_unshift () inserts the incoming unit into the array.

The code is as follows:

$ Tmparray = array ("a", "B ");
$ Resarray = array_unshift ($ tmparray, "c", "d ");
Print_r ($ resarray) // Array ([0] => a [1] => B [2] => c [3] => d)

5. array_unique de-duplicates the array and returns a new array.

The code is as follows:

$ Tmparray = ("a" => "a", "B" => "B", "c" => "c ", "d" => "B ");
$ Resarray = array_unique ($ tmparray); // ("a" => "a", "B" => "B", "c" => "c ");

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