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

How to determine whether it is a two-dimensional array in php

王林
Release: 2023-05-07 14:17:09
Original
645 people have browsed it

PHP is a server-side interpreted scripting language widely used in Web development. Its application in Web development is also very extensive, such as website development, system development, etc. Arrays are a very commonly used data type in PHP, and two-dimensional arrays are even more common data types. So, how do we determine whether an array is a two-dimensional array? This article will answer this question for you.

First of all, let’s understand and review the concept of arrays in PHP. An array is an ordered list that can store multiple values ​​under a single variable name. In PHP, there are two types of arrays, namely general arrays and associative arrays. Generally, arrays store elements using numbers as keys of the array, while associative arrays store elements using custom key names. For example:

//定义一般数组
$num = array(1,2,3,4,5);
//定义关联数组
$user = array("name"=>"Tom","age"=>20,"gender"=>"male");
Copy after login

Then there is the focus of our attention, the two-dimensional array. A two-dimensional array refers to an array containing multiple arrays, which are also called subarrays. Each subarray can contain some values ​​of its own. In PHP, two-dimensional arrays can be defined in the following ways:

//使用array表示法初始化二维数组
$users = array(
              array("name"=>"Tom","age"=>20,"gender"=>"male"),
              array("name"=>"Lucy","age"=>22,"gender"=>"female"),
              array("name"=>"Jack","age"=>18,"gender"=>"male")
          );

//使用简单的方式初始化
$users[0] = array("name"=>"Tom","age"=>20,"gender"=>"male");
$users[1] = array("name"=>"Lucy","age"=>22,"gender"=>"female");
$users[2] = array("name"=>"Jack","age"=>18,"gender"=>"male");

//使用普通数组定义二维数组
$users = array(
              ["name"=>"Tom","age"=>20,"gender"=>"male"],
              ["name"=>"Lucy","age"=>22,"gender"=>"female"],
              ["name"=>"Jack","age"=>18,"gender"=>"male"]
          );
Copy after login

Now, let’s talk about how to determine whether an array is a two-dimensional array. The author believes that there are two common methods:

Method 1: Use the is_array() function and the count() function

The is_array() function can be used to detect whether the variable is an array. If It returns true, not false. The count() function can be used to count the number of elements in an array. Taking advantage of the characteristics of these two functions, we can first use the is_array() function to determine whether the array is an array. If so, then use the count() function to determine whether the array contains multiple arrays to determine whether the array is two arrays. Dimensional array, as follows:

function is_two_dimen_array($arr){
    //判断是否为数组
    if(is_array($arr)){
        foreach($arr as $v){
            if(!is_array($v)){
                //如果数组中每个元素不是数组
                return false;
            }
        }
        //如果每个元素都是数组,返回真
        return true; 
    }
    //如果不是数组,返回假
    return false;
}

//测试
$array = array(array(1,2),array(3,4));
var_dump(is_two_dimen_array($array)); //输出bool(true)
Copy after login

In the above code, we first use the is_array() function to determine whether the array is an array. If it is an array, then traverse the elements in the array and use the is_array() function to determine each element. Whether it is an array, returns true if each element is an array, otherwise returns false.

Method 2: Use array_filter() function and array_map() function

The array_filter() function can be used to filter elements in an array that meet specific conditions and return a new array composed of these elements. The array_map() function can apply a callback function to each element in the array and return a new array. These two functions can easily operate on multi-dimensional arrays. The specific implementation is as follows:

/**
 * 判断是否为二维数组
 * @param $arr 待判断的数组
 * @return bool
 */
function is_two_dimen_array($arr){
    //筛选该数组中元素不是数组的元素
    $result = array_filter($arr,"is_array");
    //对筛选出的元素应用array_filter()函数,判断是否还存在不是数组的元素
    $result = array_map("is_array",$result);
    //如果存在该数组中的元素不是数组,返回false,否则返回true
    return !(bool)array_sum($result);
}

//测试
$array = array(array(1,2),array(3,4));
var_dump(is_two_dimen_array($array)); //输出bool(true)
Copy after login

In the above code, we first use the array_filter() function to filter the array, leaving all array elements that meet the conditions of the is_array() function, and then use the array_map() function to filter the array. It makes a judgment. If there is an element that is not an array, the corresponding element in the result set is false, otherwise it is true. Finally, use the array_sum() function to add the result sets. If the result is equal to 0, return true, otherwise return false.

To sum up, the above two methods can easily determine whether an array is a two-dimensional array. In actual development, we can choose the appropriate method to use according to the situation.

The above is the detailed content of How to determine whether it is a two-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template