【问题标题】:arrays and if statements and accessing each index position数组和 if 语句以及访问每个索引位置
【发布时间】: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


【解决方案1】:

快速解决方案

看起来您的输入数组 $count 的格式如下:

$count = [ (object) [ 'nmbr' => '0', ] ];

所以你的条件应该格式化为:

echo ($count[0]->nmbr >= 1) ? "hello" : "no", PHP_EOL;

// You want the first [0] item in the subarray of `$count` and you then want to access the property "nmbr"

工作原理

三元逻辑

上述语句中的 echo 使用了带有三元运算符的简写 if 语句。其一般形式为:

CONDITION ? TRUE : FALSE;

// You can use parentheses or not...
//   Whatever makes it easiest to read and understand;
//   all of these (and the one above) are the same

( CONDITION ) ? ( TRUE ) : ( FALSE );
( CONDITION ) ? ( TRUE ) : FALSE;
( CONDITION ) ? TRUE : FALSE;

并且等价于:

if(CONDITION){
    //Do something if TRUE
}
else{
   //Do something if FALSE
}

在此计算三元逻辑并将结果返回到echo,然后输出字符串...

echo $count[0]->nmbr >= 1 ? "hello" : "no";

注意较大循环的每次迭代 $count 看起来像

$count = [ (object) [ 'nmbr' => '0', ] ];

// Therefore...

$count[0] == (object) [ 'nmbr' => '0', ];

// and finally...

$count[0]->nmbr == 0;

true 上运行代码

有几种方法可以做到这一点:

  1. 使用函数作为真/假条件
    • 注意:你不能使用echo inside三元组
  2. 改回标准if

因此,例如,您可以执行以下任一操作:

if($count[0]->nmbr >= 1){
    // Code to carry out if true
    echo "This is true!";
}
else{
    // Code to carry out if false
    echo "false logic :(";
}

或者...

function countIsMoreThanOne() : void
{
    // Code to carry out if true
    echo "This is true!";
}

$count[0]->nmbr >= 1 ? countIsMoreThanOne() : "false logic :(";

或者...

$count[0]->nmbr >= 1 ? print("This is true!") : "false logic :(";

【讨论】:

  • 谢谢!我试过这个,我得到了错误 - “未捕获的错误:不能使用 stdClass 类型的对象作为数组”
  • @user15081222 听起来你的$count 比我想象的要深。你能复制/粘贴var_export($count)的输出吗?
  • 谢谢。抱歉,我没有澄清,$count = 实际上是 $count 的 var_dump
  • 好像不完整?应该有一个似乎没有显示的包罗万象的数组 (array(3))? var_export 表示我们可以直接复制/粘贴并使用代码
  • 这是一个非常详细的答案。再次感谢您在帮助我并为社区发布一个很好的答案方面做出了巨大的贡献
【解决方案2】:

试试这个代码:

foreach($count as $val){

    if($val >= 2){
        echo "hello";
    }
    else{
        echo "no";
    }

}

【讨论】:

  • 感谢我得到“stdClass 类的对象无法转换为 int”
  • 如果你做一个 var_dump($val); 你会得到什么死(); ?
  • OP 会得到(object) array( 'nmbr' => '4', )。如果您将其更改为$val->nmbr,它将起作用并且,如果您取出循环并设置条件$count[0]->nmbr >= 2,它也将起作用。 请参阅添加到问题中的var_export 以进行澄清
猜你喜欢
  • 2015-08-16
  • 1970-01-01
  • 2016-11-28
  • 2018-12-26
  • 2020-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-27
相关资源
最近更新 更多