$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  >  Article  >  Backend Development  >  How to detect how many one-dimensional subarrays there are in a two-dimensional array in PHP

How to detect how many one-dimensional subarrays there are in a two-dimensional array in PHP

青灯夜游
青灯夜游Original
2022-09-26 19:15:373650browse

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 ;}".

How to detect how many one-dimensional subarrays there are in a two-dimensional array in PHP

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);

How to detect how many one-dimensional subarrays there are in a two-dimensional array in PHP

$arr = array(array(2),array(4),array(6),array(8)); 
var_dump($arr);
f($arr);

How to detect how many one-dimensional subarrays there are in a two-dimensional array in PHP


Description: is_array() function

in_array() function Searches an array for a specified value. Syntax format:

in_array ( $search , $array ,$strict)
##ParameterDescriptionRequired. Specifies the value to search for in the array. Required. Specifies the array to search.             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.
search
array
strict
Return value: TRUE if the value is found in the array, FALSE otherwise.


Recommended learning: "

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!

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