Home > Article > Backend Development > How to intercept a two-dimensional array in php
PHP is a very commonly used scripting language. In daily development, we may often need to intercept two-dimensional arrays. It is very convenient to use PHP to operate two-dimensional arrays. Here are several ways to intercept two-dimensional arrays.
1. Use the array_slice() function
The array_slice() function is a function that comes with PHP and can be used to intercept part of the array. Using this function, you can easily intercept a two-dimensional array. When using it, you only need to specify the starting position and length of the interception.
The usage method is as follows:
array array_slice ( array $array , int $offset , int $length = null , bool $preserve_keys = false )
Among them, $array is the array to be intercepted, $offset is the starting position of interception, $length is the length of interception, and $preserve_keys indicates whether to retain the original The key name, the default is false.
It should be noted that this function will return a new array and will not affect the value of the original array. The following is an example:
$arr = array( array('name'=>'张三','age'=>20), array('name'=>'李四','age'=>22), array('name'=>'王五','age'=>24), array('name'=>'赵六','age'=>26), array('name'=>'孙七','age'=>28) ); $new_arr = array_slice($arr, 1,2); print_r($new_arr);
The output result is:
Array ( [0] => Array ( [name] => 李四 [age] => 22 ) [1] => Array ( [name] => 王五 [age] => 24 ) )
The above code intercepts the two elements starting from the second element in the original array and returns a new array.
2. Use for loop
Using "for loop" can also intercept a part of the two-dimensional array, and achieve interception by controlling the starting position and ending position of the loop. The following is an example:
$arr = array( array('name'=>'张三','age'=>20), array('name'=>'李四','age'=>22), array('name'=>'王五','age'=>24), array('name'=>'赵六','age'=>26), array('name'=>'孙七','age'=>28) ); $new_arr = array(); for($i=1;$i<=2;$i++){ array_push($new_arr,$arr[$i]); } print_r($new_arr);
The output result is the same as the previous example:
Array ( [0] => Array ( [name] => 李四 [age] => 22 ) [1] => Array ( [name] => 王五 [age] => 24 ) )
The above code uses the "for loop" to intercept two elements starting from the second element and return a new array.
3. Use the array_splice() function
The array_splice() function is also a function that comes with PHP and can be used to intercept part of the array. Using this function, you can easily intercept a two-dimensional array. When using it, you only need to specify the starting position and length of the interception.
The usage method is as follows:
array array_splice ( array &$input , int $offset [, int $length = 0 [, mixed $replacement = array() ]] )
Among them, $input is the array to be intercepted, $offset is the starting position of interception, $length is the length of interception, and $replacement represents the replacement content. Can be an array or other type of value.
It should be noted that this function will modify the value of the original array, that is, after interception, the original array will be changed. The following is an example:
$arr = array( array('name'=>'张三','age'=>20), array('name'=>'李四','age'=>22), array('name'=>'王五','age'=>24), array('name'=>'赵六','age'=>26), array('name'=>'孙七','age'=>28) ); array_splice($arr, 1,2); print_r($arr);
The output result is:
Array ( [0] => Array ( [name] => 张三 [age] => 20 ) [1] => Array ( [name] => 赵六 [age] => 26 ) [2] => Array ( [name] => 孙七 [age] => 28 ) )
The above code intercepts the two elements starting from the second element in the original array, and modifies the original array.
4. Using array_slice() and foreach loop
Sometimes we need to intercept a certain column in the two-dimensional array, which can be achieved by using the array_slice() function and foreach loop. The following is an example:
$arr = array( array('name'=>'张三','age'=>20), array('name'=>'李四','age'=>22), array('name'=>'王五','age'=>24), array('name'=>'赵六','age'=>26), array('name'=>'孙七','age'=>28) ); $new_arr = array(); foreach($arr as $val){ $new_arr[] = array_slice($val,0,1); } print_r($new_arr);
The output result is:
Array ( [0] => Array ( [name] => 张三 ) [1] => Array ( [name] => 李四 ) [2] => Array ( [name] => 王五 ) [3] => Array ( [name] => 赵六 ) [4] => Array ( [name] => 孙七 ) )
The above code intercepts the first column in the two-dimensional array and returns a new array.
The above are several methods of intercepting two-dimensional arrays. I hope they can help you solve problems in daily development.
The above is the detailed content of How to intercept a two-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!