【发布时间】:2021-01-30 21:51:29
【问题描述】:
我不确定我是否理解 if 语句以及它们如何访问数组以及我想要的。我有这个数组:
如果我var_dump($count) 我得到:
array(1) { [0]=> object(stdClass)#2472 (1) { ["nmbr_tables"]=> string(1) "4" } } array(1) { [0]=> object(stdClass)#2445 (1) { ["nmbr_tables"]=> string(1) "0" } } array(1) { [0]=> object(stdClass)#2452 (1) { ["nmbr_tables"]=> string(1) "0" } }
所以我写
if($count >= 1){echo "hello";} else {echo "no";};
我的预期输出是:
hello
no
no
由于第一个索引位置的 nmbr 为“4”,接下来的两个索引位置的 nmbr 为“0”
相反,我得到的是:
hellohellohello
似乎表明它只关注第一个索引位置。
然后我想也许:
foreach($count as $val){
if($count >= 2){echo "hello";} else {echo "no";};
};
没有区别。
如果我 var_export($count) 我得到:
array ( 0 => (object) array( 'nmbr' => '4', ), )array ( 0 => (object) array( 'nmbr' => '0', ), )array ( 0 => (object) array( 'nmbr' => '0', ), )
【问题讨论】:
-
$count是您在foreach中的数组。您需要查看$val。第一个上下文不清楚,静态调用只会输出 1 个值,而不是 3 个。 -
谢谢我在使用 foreach 时得到“stdClass 类的对象无法转换为 int”
标签: php arrays if-statement