In PHP, array is a very important data type, which can store a set of ordered elements, which can be strings, integers, floating point numbers, Boolean values, etc. In PHP, arrays are divided into two types: one-dimensional arrays and multi-dimensional arrays. A one-dimensional array is an ordinary array, which contains only one dimension, while a multi-dimensional array is an array containing two or more dimensions.
Sometimes we need to determine whether an array is a two-dimensional array, because there is a big difference between a two-dimensional array and a one-dimensional array. If we do not distinguish them, some errors may occur at runtime. Let's discuss how to determine whether an array is a two-dimensional array.
Method 1: Use the count function
In PHP, you can use the count function to get the length of an array, that is, the number of elements in the array. If this array is a one-dimensional array, then the count function returns the number of array elements; if this array is a multi-dimensional array, then the count function returns the number of first-dimensional elements of the multi-dimensional array.
Therefore, we can determine whether the array is a two-dimensional array by judging whether the length of the first element of the array is equal to the length of the array. The specific code is as follows:
function is_two_dimensional_array($array) { if (!is_array($array)) { return false; } if (!isset($array[0]) || !is_array($array[0])) { return false; } return (count($array) == count($array, COUNT_RECURSIVE) / count($array)); }
In the above code, we first use the is_array function to check whether the parameter is an array, and if not, return false directly. Next, we use the isset function to check if the first element of the array exists and if it is an array, and if not, returns false. Finally, we use the count function to calculate the product of the length of the entire array and the length of the first element. If they are equal, it means it is a two-dimensional array, otherwise it is not.
The following is a test case:
$array1 = array(); $array2 = array('a', 'b', 'c'); $array3 = array(array('a', 'b'), array('c', 'd')); $array4 = array(array('a', 'b'), 'c'); var_dump(is_two_dimensional_array($array1)); // false var_dump(is_two_dimensional_array($array2)); // false var_dump(is_two_dimensional_array($array3)); // true var_dump(is_two_dimensional_array($array4)); // false
Method 2: Use the foreach function
In addition to using the count function, you can also use the foreach function to traverse the array to determine whether Contains array elements. If found, it can be determined that the array is a two-dimensional array.
The specific code is as follows:
function is_two_dimensional_array($array) { if (!is_array($array)) { return false; } foreach ($array as $element) { if (is_array($element)) { return true; } } return false; }
In the above code, we first check whether the parameter is an array, and if not, return false directly. Next, we use the foreach function to traverse the array and check whether each element is an array. If so, it means that the array is a two-dimensional array and returns true directly. If the array element is not found at the end of the traversal, false is returned.
The following is a test case:
$array1 = array(); $array2 = array('a', 'b', 'c'); $array3 = array(array('a', 'b'), array('c', 'd')); $array4 = array(array('a', 'b'), 'c'); var_dump(is_two_dimensional_array($array1)); // false var_dump(is_two_dimensional_array($array2)); // false var_dump(is_two_dimensional_array($array3)); // true var_dump(is_two_dimensional_array($array4)); // false
No matter which method is used, it is relatively easy to determine whether an array is a two-dimensional array. However, in actual development, we should also pay attention to other factors such as array subscripts and element types to ensure that the program can run normally.
The above is the detailed content of PHP determines whether it is a two-dimensional array. For more information, please follow other related articles on the PHP Chinese website!