Home  >  Article  >  Backend Development  >  [PHP] Introduction and examples of the array_merge() function and array_chunk() function of arrays

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

little bottle
little bottleOriginal
2019-04-19 17:43:462734browse

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.​

<?php
    $array1=array("color"=>"red",2,4);
    $array2=array("a","b","color"=>"green","shape"=>"trapezoid",4);
    $result=array_merge($array1,$array2);
    echo "<pre class="brush:php;toolbar:false">";
    print_r($result);
    echo "
"; /*运行结果: Array ( [color] => green [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4 ) */ ?>

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:

<?php
    $arr=array("cctv-a","cctv-b","cctv-c");
    //分割数组
    echo "<pre class="brush:php;toolbar:false">";
    echo "分割后的数组为:" . "<br/>";
    print_r(array_chunk($arr,2));
    echo "
"; /*运行结果: 分割后的数组为: Array ( [0] => Array ( [0] => cctv-a [1] => cctv-b ) [1] => Array ( [0] => cctv-c ) ) */ //第二次分割 echo "
";
   echo "分割后的数组为:" . "<br/>";
   print_r(array_chunk($arr,2,true));
   echo "
"; /*运行结果: 分割后的数组为: Array ( [0] => Array ( [0] => cctv-a [1] => cctv-b ) [1] => Array ( [2] => cctv-c ) ) */ ?>

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!

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