How does PHP determine if an array is two-dimensional

There are two ways PHP can determine if an array is two-dimensional

Method 1: Use the count() function to find out

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

count($arr) != count($arr, 1)
<!--?</span--> php
header('content-type:text/html; charset=utf-8');
$arr = array (1, array (2, 4), 6);
if (count($arr) ! = count($arr, 1)) {
Echo 'is a two-dimensional array ';
} else {
Echo 'not a two-dimensional array ';
? >

Method 2: The foreach statement +is_array() function

Use the foreach statement to loop through the groups

In the body of the loop, the is_array() function is used to determine whether the element values are of array type. If neither is, it is not a two-dimensional array, and if either is, it is a two-dimensional array

$arr = array(1,2,3,4,5); 
foreach($arr as $v){
    echo "是二维数组";
    echo "不是二维数组";

Leave a Reply

Your email address will not be published. Required fields are marked *