Common Array processing methods in PHP example analysis _ PHP Tutorial

Source: Internet
Author: User
Examples of commonly used array processing methods in PHP. 1. 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. Copy the code as follows :? $ Tmparrayarray (1, 1. 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 ");
?>

The http://www.bkjia.com/PHPjc/319439.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/319439.htmlTechArticle1.array_shift () removes the first unit of the array and returns the result, which is convenient for the first unit of the array to be processed separately. The code is as follows :? $ Tmparray = array ("1 ",...

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.