php 是变量数组或对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10957100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:27:28  来源:igfitidea点击:

php is variable an array or object

phparraysstringobject

提问by chris

Trying to figure out how to do the equivalent of something I did in javascript but in php. But I'm not sure of the operators to do it. In javascript I wanted to see if a particular parameter being passed was either an object or array.. and if not then was it a string/int and what I did was something like

试图弄清楚如何做相当于我在 javascript 但在 php 中所做的事情。但我不确定运营商会这样做。在javascript中,我想查看传递的特定参数是对象还是数组..如果不是,那么它是一个字符串/整数,我所做的就像

if (str instanceof Array || str instanceof Object) 
{
   //code
}
else
{
   //code
}

anyone know of the equivalent to this for php?

任何人都知道这相当于 php 吗?

回答by Ry-

Use is_arrayto check if a variable is an array, and similarly, use is_objectto check if a variable is an object.

使用is_array检查一个变量是一个数组,并且类似地,使用is_object检查一个变量是一个对象。

回答by WOLFF

Try to use:

尝试使用:

if (!is_scalar($var)) {
    // Varible is object or array
}

回答by Yani

I came across this question while looking for is_countable. Maybe it's of some use to somebody. https://www.php.net/manual/en/function.is-countable.php

我在寻找is_countable. 也许它对某人有用。 https://www.php.net/manual/en/function.is-countable.php

回答by Vishal Mohan

object (use is_object)-----

对象(使用 is_object)-----

stdClass Object
(
    [rest_food_items_id] => 137
    [rest_user_id] => 42
)

array (use is_array)----

数组(使用 is_array)----

Array
(
    [rest_food_items_id] => 137
    [rest_user_id] => 42
)

**

**

Example

例子

**

**

if(is_object($data)){

}
if(is_array($data)){

}