[PHP] Introduction and examples of the array_merge() function and array_chunk() function of arrays

little bottle
Release: 2023-04-06 06:52:01
Original
2565 people have browsed it

This article mainly talks about the merge and split functions in PHP. They are array_merge() function and array_chunk() function respectively. Let’s get to know these two functions with the editor!

php arrayIntegration and splitting

1. array_merge() function

The function of array_merge() function is Merge one or more arrays.

   array array_merge(array $array1 [,array $...]);  
Copy after login

array_merge() merges the cells of one or more arrays, appends the value of one array to another array, and returns a new array.

 ①If the input array contains the same string key name, the value after the key name will overwrite the previous value;
 ②If the array contains a numeric key name, the subsequent value will not Overwrite the original value and append it to the end of the array;
 ③If the array is numerically indexed, the key names will be re-indexed in a continuous manner.​

"red",2,4);
    $array2=array("a","b","color"=>"green","shape"=>"trapezoid",4);
    $result=array_merge($array1,$array2);
    echo "
";
    print_r($result);
    echo "
"; /*运行结果: Array ( [color] => green [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4 ) */ ?>
Copy after login

2. array_chunk() function

Function array_chunk() is used to split an array into multiple arrays.

   array array_chunk(array $input,int $size [,bool $preserve_keys]);   
Copy after login

In the above statement, $input represents the array to be split, $size is the number of elements in each array after splitting, and $preserve_keys is an optional parameter. If this parameter is set to true, the elements in the split array will retain the original index. If this parameter is set to false, the index of the elements in the split array will start from zero.

The code is as follows:

";
    echo "分割后的数组为:" . "
"; print_r(array_chunk($arr,2)); echo "
"; /*运行结果: 分割后的数组为: Array ( [0] => Array ( [0] => cctv-a [1] => cctv-b ) [1] => Array ( [0] => cctv-c ) ) */ //第二次分割 echo "
";
   echo "分割后的数组为:" . "
"; print_r(array_chunk($arr,2,true)); echo "
"; /*运行结果: 分割后的数组为: Array ( [0] => Array ( [0] => cctv-a [1] => cctv-b ) [1] => Array ( [2] => cctv-c ) ) */ ?>
Copy after login

If you want to know more PHP learning tutorials, please pay attention to the PHP video tutorial on the PHP Chinese website and learn in depth with the teacher!

The above is the detailed content of [PHP] Introduction and examples of the array_merge() function and array_chunk() function of arrays. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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 [email protected]
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!