php、mysql、数组 - if( x == 0 ) { }

发布于 2024-10-10 23:25:30 字数 665 浏览 2 评论 0原文

我有以下代码

while($row = $usafisRSP->fetch_assoc())
{

 $hidden_keys  = array('Applicantid', 'unique_num', 'regs_time' ....);
 $hidden_fields = array_intersect_key($row, array_fill_keys($hidden_keys, NULL));

$hidden_values = array();

foreach ($hidden_fields as $key => $value) {
  // fill the values array using the values from fields array
  $hidden_values[$value] =  "$key = ".base64_decode($value)."
"; if(base64_decode($value)== 0) { $hidden_values[$value] = ""; } echo $hidden_values[$value];

问题是关于“if($hidden_​​values[$value] == 0)”...基本上我想不显示/回显 $hidden_​​values[$value] 如果 $value 的值为 0有时候$value是0或者一些像(23 avenue)这样的词。

I have the following code

while($row = $usafisRSP->fetch_assoc())
{

 $hidden_keys  = array('Applicantid', 'unique_num', 'regs_time' ....);
 $hidden_fields = array_intersect_key($row, array_fill_keys($hidden_keys, NULL));

$hidden_values = array();

foreach ($hidden_fields as $key => $value) {
  // fill the values array using the values from fields array
  $hidden_values[$value] =  "$key = ".base64_decode($value)."
"; if(base64_decode($value)== 0) { $hidden_values[$value] = ""; } echo $hidden_values[$value];

The question is about "if($hidden_values[$value] == 0)" ... Basically I want to do not display/echo the $hidden_values[$value] if it's value of $value is 0. Sometimes $value is 0 or some words like (23 avenue).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千纸鹤带着心事 2024-10-17 23:25:30

我认为您在 PHP 类型比较和相等方面遇到了三个问题:

  1. 任何不以数字开头的字符串总是松散地等于 0。所以基本上,if(base64_decode($value)== 0) 可能总是解析为 true,即使已解码$value“Adam”
  2. base64_decode的返回值是一个字符串,所以如果结果为0,它将是string 0,而不是integer 0。这意味着if(base64_decode($value ) === 0) 如果解码后的 $value"0" 甚至无法工作。另一个问题是,base64_decode 可能会在错误时返回 false,再次未能通过严格的相等性检查。
  3. 非空字符串("0" 除外)始终松散地等于 true。因此,这是您的案例真正需要的唯一比较。

我认为这就是你想要的,替换最后 5 行......

if(base64_decode($value)) echo $hidden_values[$value];
else $hidden_values[$value] = "";

} // closing your for loop

I think you ran into three catches with PHP type comparisons and equalities:

  1. Any string not beginning with a number will always loosely equal 0. So basically, if(base64_decode($value)== 0) will likely always resolve to true, even if decoded $value is "Adam".
  2. Return value of base64_decode is a string, so if 0 is the result, it will be string 0, not integer 0. This means if(base64_decode($value) === 0) wouldn't even work if decoded $value is "0". Another catch is base64_decode may return false on errors, again failing this strict equality check.
  3. A non-empty string (other than "0") will always loosely equal true. So this is the only comparison you really need for your case.

I think this is what you want, replacing the last 5 lines...

if(base64_decode($value)) echo $hidden_values[$value];
else $hidden_values[$value] = "";

} // closing your for loop
泪之魂 2024-10-17 23:25:30

这是您要找的吗?

foreach( $hidden_values as $value ) {
    if( $value !== 0 ) {
        echo $value;
    }
}

Is this what you're looking for?

foreach( $hidden_values as $value ) {
    if( $value !== 0 ) {
        echo $value;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文