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 $...]);
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]);
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 "分割后的数组为:" . ""; /*运行结果: 分割后的数组为: Array ( [0] => Array ( [0] => cctv-a [1] => cctv-b ) [1] => Array ( [0] => cctv-c ) ) */ //第二次分割 echo "
"; print_r(array_chunk($arr,2)); echo "
"; echo "分割后的数组为:" . ""; /*运行结果: 分割后的数组为: Array ( [0] => Array ( [0] => cctv-a [1] => cctv-b ) [1] => Array ( [2] => cctv-c ) ) */ ?>
"; print_r(array_chunk($arr,2,true)); echo "
If you want to know more PHP learning tutorials, please pay attention to thePHP video tutorialon 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!