Home >Backend Development >PHP Problem >How to determine whether an array is a two-dimensional array in PHP

How to determine whether an array is a two-dimensional array in PHP

青灯夜游
青灯夜游Original
2022-09-07 19:25:503539browse

Two judgment methods: 1. Use the count() function to compare whether the length obtained is the same when the second parameter is omitted and when the second parameter is not omitted. The syntax "count($arr)!=count( $arr,1)", if the return value is true, it is a two-dimensional array, and vice versa. 2. Use the foreach statement to loop through the array, and use is_array() in the loop body to determine whether the element value is an array type. If neither element is of array type, it is not a two-dimensional array. If one is, it is a two-dimensional array.

How to determine whether an array is a two-dimensional array in PHP

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

php determines whether the array is Two methods for two-dimensional arrays

Method 1: Use the count() function to determine

The count() function calculates the number in the array The number of units or the number of attributes in the object

count ( mixed $var [, int $mode ] )

$mode: is an optional parameter and can be omitted.

  • If the $mode parameter is omitted, or set to COUNT_NORMAL or 0, the count() function will not detect multidimensional arrays;

  • If If $mode is set to COUNT_RECURSIVE or 1, the count() function will recursively count the number of elements in the array, which is especially useful for calculating the number of elements in multi-dimensional arrays.

If the $mode parameter is omitted, count will not detect multi-dimensional arrays and will only obtain the number of elements in one dimension.

So you only need to compare whether the length obtained when the $mode parameter is omitted and when the $mode parameter is not omitted is the same to determine whether it is a two-dimensional array.

count($arr) != count($arr, 1)
  • If they are not equal (the return value is true), it is a two-dimensional array

  • If they are equal (the return value is false) ), it is not a two-dimensional array

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr = array(1,array(2,4),6); 
var_dump($arr);
if (count($arr) != count($arr, 1)) {
    echo &#39;是二维数组&#39;;
} else {
    echo &#39;不是二维数组&#39;;
} 
?>

How to determine whether an array is a two-dimensional array in PHP

Method 2: foreach statement is_array() function

  • Use the foreach statement to loop through the array

  • In the loop body, use the is_array() function to determine whether the element value is an array type, if not, then it is not Two-dimensional array, if there is one, it is a two-dimensional array

<?php  
header("content-type:text/html;charset=utf-8");
$arr = array(1,2,3,4,5); 
var_dump($arr);
$con=0;
foreach($arr as $v){
	if(is_array($v)){
		$con=1;
		break;
	}else{
		$con=0;
	}
}
if($con==1){
	echo "是二维数组";
}else{
	echo "不是二维数组";
}

?>

How to determine whether an array is a two-dimensional array in PHP

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to determine whether an array is 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