如何在if条件下检查数组值? [英] How to check the array value in if condition?

查看:277
本文介绍了如何在if条件下检查数组值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用codeignator,并且正在数组中获得多个值。

I am using codeignator and I am getting the multiple value in the array.

$status = $this->input->post('Status[]');
print_r($status);

输出为

Array ( 
[0] => 1 
[1] => 7 
[2] => 8 
[3] => 7 
)
It will increase and value will be duplicate like 7.

现在,我必须检查每个数组的值。所以我尝试了

Now I have to check each array value. so I tried

if (($status=1)||($status=3) || ($status=6) || ($status=8)|| ($status=9)) {
    $remark = $this->input->post('remark[]');
   }
   else{$remark="";}

if(($status=2)||($status=4) || ($status=5)){
    $reasonDate = $this->input->post('reasonDate[]');
    $remark = $this->input->post('remark[]');
     }
  else{
       $reasonDate="";
       $remark="";
}

if($status=7){
   $reasonA = $this->input->post('reasonA[]');
   $reason = $this->input->post('reason[]');
   }
   else{
        $reasonAmt="";
        $reason="";
 }

您能帮我如何通过if条件检查数组值吗?我是否需要使用in_array或其他任何方法?

Can you help me out how to check the array value with if condition? Should I need to use in_array or any other way?

推荐答案

目前尚不清楚100%您正在尝试做什么,所以希望以下答案之一会有所帮助。

It's not 100% clear what you're attempting to do, so hopefully one of the answers below will help.

如果您要遍历数组中的每个状态并检查其是否为特定值,则可以使用foreach循环。看起来像这样:

If you're trying to go through each status in the array and check if it is a specific value, you can use a foreach loop. It would look something like this:

$statuses = [1, 7, 8, 7];

foreach ($statuses as $status) {
    if ($status=1 || $status=3 || $status=6 || $status=8 || $status=9) {
        // Do something
    }
    else {
        // Do something else
    }
    // etc...
}

如果您要检查数组中至少一个状态是否为特定状态值,则可以使用Dave建议的 in_array函数。看起来像这样:

If you're trying to check if at least one of the statuses in the array is a specific value, you can use the "in_array" function like Dave suggested. It would look something like this:

$statuses = [1, 7, 8, 7];

if (in_array(1, $statuses) || in_array(3, $statuses) || in_array(6, $statuses) || in_array(8, $statuses) || in_array(9, $statuses)) {
    // Do something
}
else {
    // Do something else
}
// etc...

您可以查找 in_array函数的文档此处。

You can look up the documentation for the "in_array" function here.

这篇关于如何在if条件下检查数组值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆