$v){//loop body code}"; 3. In the loop body, use the is_array() function and the "++" operator to count the one-dimensional subdivisions in the two-dimensional array The number of arrays, syntax "if(is_array($v)){$num++;}"."/> $v){//loop body code}"; 3. In the loop body, use the is_array() function and the "++" operator to count the one-dimensional subdivisions in the two-dimensional array The number of arrays, syntax "if(is_array($v)){$num++;}".">
Home >Backend Development >PHP Problem >How to detect how many one-dimensional subarrays there are in a two-dimensional array in PHP
Detection steps: 1. Define an array and assign it a value of 0 to store the number of one-dimensional sub-arrays. The syntax is "$num=0;"; 2. Use the foreach statement to loop through the outer part of the two-dimensional array. Layer array elements, syntax "foreach($arr as $k => $v){//loop body code}"; 3. In the loop body, use the is_array() function and the " " operator to count two-dimensional The number of one-dimensional subarrays in an array, syntax "if(is_array($v)){$num ;}".
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer
In PHP, you can use the foreach statement and is_array () function to count the number of one-dimensional subarrays in a two-dimensional array.
Implementation steps:
Step 1: Define an array and assign it a value of 0, used to store the number of one-dimensional subarrays
$num=0;
Step 2: Use the foreach statement to loop through the outer array elements of the two-dimensional array
foreach($arr as $k => $v){ //循环体代码 }
Traverse the given $arr array, in each loop, the value of the current array will be assigned to $v, and the key name will be assigned to $k.
Step 3: In the loop body, use the is_array() function and the " " operator to count the number of one-dimensional sub-arrays in the two-dimensional array
Use the is_array() function to detect whether the current element is an array type (that is, whether it is a one-dimensional subarray)
If so, use the " " operator to change the $num variable Add 1 to the value
if(is_array($v)){ $num++; }
Full implementation code:
function f($arr){ $num=0; foreach($arr as $v){ if(is_array($v)){ $num++; } } echo "一维子数组的数量为:".$num; }
Call the above f($arr) function
$arr = array(1,2,3,array(4,5,6),7,8,array(9,10)); var_dump($arr); f($arr);
$arr = array(array(2),array(4),array(6),array(8)); var_dump($arr); f($arr);
Description: is_array() function
in_array() function Searches an array for a specified value. Syntax format:
in_array ( $search , $array ,$strict)
Description | |
---|---|
search | Required. Specifies the value to search for in the array.|
array | Required. Specifies the array to search.|
strict | Optional. If this parameter is set to TRUE, the in_array() function checks whether the data being searched is of the same type as the value of the array.
PHP Video Tutorial", "PHP ARRAY"
The above is the detailed content of How to detect how many one-dimensional subarrays there are in a two-dimensional array in PHP. For more information, please follow other related articles on the PHP Chinese website!