Home >Backend Development >PHP Problem >How to cut an array in php? Introduction to built-in functions
In PHP, sometimes we need to cut an array according to certain rules, such as cutting according to the value of a certain field, or cutting according to a certain length, etc. At this time, we can use some functions provided in PHP to complete this task. This article will introduce how to use PHP functions to cut arrays.
1. Use the array_chunk() function to cut the array
The array_chunk() function can cut an array according to the specified length, and each cut part is saved in a new array item in. The specific syntax is as follows:
array array_chunk(array $array, int $size, bool $preserve_keys = false)
The meaning of each parameter is as follows:
For example, if there is an array $nums=['1','2','3','4','5','6','7','8' ,'9'], we want to slice this array according to each array item containing 3 elements, you can use the following code:
$new_array = array_chunk($nums,3); print_r($new_array);
The output result is as follows:
Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [1] => Array ( [0] => 4 [1] => 5 [2] => 6 ) [2] => Array ( [0] => 7 [1] => 8 [2] => 9 ) )
2. Use array_slice () function to slice the array
array_slice() function can slice the array according to the specified index range and return the sliced array. The specific syntax is as follows:
array array_slice(array $array, int $offset, int $length = null, bool $preserve_keys = false)
The meaning of each parameter is as follows:
For example, if there is an array $nums=['1','2','3','4','5','6','7','8' ,'9'], we want to cut this array from the 4th element to the 7th element, you can use the following code:
$new_array = array_slice($nums,3,4); print_r($new_array);
The output result is as follows:
Array ( [0] => 4 [1] => 5 [2] => 6 [3] => 7 )
3. Use The array_filter() function is used to cut the array
The array_filter() function can filter an array according to the specified rules and return an array that meets the conditions. The specific syntax is as follows:
array array_filter(array $array, callable $callback = null, int $flag = 0)
The meaning of each parameter is as follows:
For example, if there is an array $nums=['1','2','3','4','5','6','7','8' ,'9'], we want to filter out odd elements from this array, we can use the following code:
$new_array = array_filter($nums,function($var){return ($var%2==1);}); print_r($new_array);
The output result is as follows:
Array ( [0] => 1 [2] => 3 [4] => 5 [6] => 7 [8] => 9 )
4. Use foreach loop to cut the array
In addition to using the functions provided by PHP to cut the array, we can also use the foreach loop to complete this task. The specific code is as follows:
$nums=['1','2','3','4','5','6','7','8','9']; $new_array=array(); $temp_array=array(); foreach ($nums as $key=>$value){ $temp_array[]=$value; if(($key+1)%3==0||$key==count($nums)-1){ $new_array[]=$temp_array; $temp_array=array(); } } print_r($new_array);
The output result is as follows:
Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [1] => Array ( [0] => 4 [1] => 5 [2] => 6 ) [2] => Array ( [0] => 7 [1] => 8 [2] => 9 ) )
The above are the four methods of using PHP to cut arrays. You can choose the appropriate method to complete the task according to the actual situation.
The above is the detailed content of How to cut an array in php? Introduction to built-in functions. For more information, please follow other related articles on the PHP Chinese website!