检查值是否为多维数组[重复]

时间:2022-05-28 01:28:43

This question already has an answer here:

这个问题在这里已有答案:

How come when I run this code, I get an output of I am a multidimensional array! (the first block). I thought it would go into the second block, but it doesn't. What am I missing here?

为什么我运行这段代码,我得到一个输出,我是一个多维数组! (第一块)。我以为它会进入第二块,但事实并非如此。我在这里想念的是什么?

$values = array('1','2');
if(isset($values[0][0])){
    echo "I am a multidimensional array!";
}else{
    echo "I am not a multidimensional array.";
}

3 个解决方案

#1


3  

$values = array(1,array(1,2));


$multi = false;
if(is_array($values)){
    foreach($values as $k=>$v){
        if(is_array($v)){
            $multi = true;
            break;
        }
    }
}

echo $multi ? "multi" : "not multi";

#2


3  

Try this:

尝试这个:

if(is_array($values[0]))

Edit: This will check the first element of the array only. You should loop through each element to check if its truly multidimensional.

编辑:这将仅检查数组的第一个元素。您应该遍历每个元素以检查它是否真正是多维的。

This code checks to see if the first element of the array is also an array. isset just checks whether or not a variable is NULL.

此代码检查数组的第一个元素是否也是一个数组。 isset只检查变量是否为NULL。

isset in your example is not working as expected. Perhaps there is a slight difference in functionality between PHP versions or setups. I didn't see anything in the manual but maybe you can:

您的示例中的isset未按预期工作。也许PHP版本或设置之间的功能略有不同。我手册中没有看到任何内容,但也许您可以:

http://php.net/manual/en/function.isset.php

http://php.net/manual/en/function.isset.php

Using is_array is more semantic, so in my opinion is a much better choice.

使用is_array更具语义性,所以在我看来是一个更好的选择。

#3


1  

This code only goes into the if-branch for me, if the first value in the array is explicitly declared as a string,

如果数组中的第一个值显式声明为字符串,则此代码仅进入if分支,

$values = array('1',2);

– and with that the behavior is nothing but logical, because $values[0] is that text literal '1', and that has a first character that can be access using a zero based index.

- 并且这种行为只不过是合乎逻辑的,因为$ values [0]是文本文字'1',并且具有可以使用基于零的索引访问的第一个字符。

So I guess either your real data is of a string type – or it maybe depends in the PHP version (I tested under 5.3.16).

所以我猜你的真实数据是字符串类型 - 或者它可能取决于PHP版本(我在5.3.16下测试)。

Anyway, using is_array as the other answers already suggested is the right way to go here.

无论如何,使用is_array已经建议的其他答案是正确的方法。

#1


3  

$values = array(1,array(1,2));


$multi = false;
if(is_array($values)){
    foreach($values as $k=>$v){
        if(is_array($v)){
            $multi = true;
            break;
        }
    }
}

echo $multi ? "multi" : "not multi";

#2


3  

Try this:

尝试这个:

if(is_array($values[0]))

Edit: This will check the first element of the array only. You should loop through each element to check if its truly multidimensional.

编辑:这将仅检查数组的第一个元素。您应该遍历每个元素以检查它是否真正是多维的。

This code checks to see if the first element of the array is also an array. isset just checks whether or not a variable is NULL.

此代码检查数组的第一个元素是否也是一个数组。 isset只检查变量是否为NULL。

isset in your example is not working as expected. Perhaps there is a slight difference in functionality between PHP versions or setups. I didn't see anything in the manual but maybe you can:

您的示例中的isset未按预期工作。也许PHP版本或设置之间的功能略有不同。我手册中没有看到任何内容,但也许您可以:

http://php.net/manual/en/function.isset.php

http://php.net/manual/en/function.isset.php

Using is_array is more semantic, so in my opinion is a much better choice.

使用is_array更具语义性,所以在我看来是一个更好的选择。

#3


1  

This code only goes into the if-branch for me, if the first value in the array is explicitly declared as a string,

如果数组中的第一个值显式声明为字符串,则此代码仅进入if分支,

$values = array('1',2);

– and with that the behavior is nothing but logical, because $values[0] is that text literal '1', and that has a first character that can be access using a zero based index.

- 并且这种行为只不过是合乎逻辑的,因为$ values [0]是文本文字'1',并且具有可以使用基于零的索引访问的第一个字符。

So I guess either your real data is of a string type – or it maybe depends in the PHP version (I tested under 5.3.16).

所以我猜你的真实数据是字符串类型 - 或者它可能取决于PHP版本(我在5.3.16下测试)。

Anyway, using is_array as the other answers already suggested is the right way to go here.

无论如何,使用is_array已经建议的其他答案是正确的方法。