Examples of common array processing methods in PHP _php techniques

Source: Internet
Author: User
1.array_shift () Moves the first cell of the array back again, which is convenient for the first unit of the array to be processed separately.

Copy Code code as follows:

?
$tmparray = Array ("1", "2", "3", "4");
$tmparray = Array_shift ($tmparray);
Print_r ($tmparray);
?>


2.array_chunk () divides an array into multiple arrays, followed by a parameter that controls the number of array cells.

Copy Code code as follows:

?
$tmparray = Array (' A ', ' B ', ' C ', ' d ', ' e ');
Print_r (Array_chunk ($tmparray, 2));
?>

This function works well in some loops, such as I want to put the obtained data into n rows, m columns. If we do not cycle directly, of course, with nested loops can be implemented, but too troublesome, if the first use of array_chunk () This function first processing and then return the new array, it is very convenient.
3.array_push () presses the passed-in value into the end of the array.

Copy Code code 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 cell as a whole into the array

Copy Code code as follows:

<?php
$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 the array to go heavy and return a new one

Copy Code code 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: 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.