Home  >  Article  >  Backend Development  >  Example Analysis of Commonly Used Array Processing Methods in PHP_PHP Tutorial

Example Analysis of Commonly Used Array Processing Methods in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:49:551018browse

1.array_shift() moves the first unit of the array out and then returns it. This is very convenient for sometimes the first unit of the array needs to be processed separately.

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

Copy code The code is as follows:

$tmparray = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($tmparray, 2));
?>

This function has some Loops are very useful. For example, I want to put the obtained data into N rows and M columns. It will definitely not work if we loop directly. Of course, we can use nested loops to achieve it, but it is too troublesome. It would be very convenient if we use the array_chunk() function to process it first and then return the new array.
3.array_push() pushes the incoming value to the end of the array.

Copy code 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 entire incoming unit into the array

Copy code 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 deduplicates the array and returns a new array

Copy code The code is as follows:

$tmparray = ("a" => "a","b" => ; "b","c" => "c","d" => "b");
$resarray = array_unique($tmparray);//("a" => "a" ,"b" => "b","c" => "c");
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319439.htmlTechArticle1.array_shift() moves the first unit of the array out and then returns it. This is sometimes useful for the first unit of the array. It is convenient to handle each unit individually. Copy the code The code is as follows: ? $tmparray = array("1",...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn