问题
I am using codeignator and I am getting the multiple value in the array.
$status = $this->input->post('Status[]');
print_r($status);
Output is
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="";
}
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?
回答1:
It's not 100% clear what you're attempting to do, so hopefully one of the answers below will help.
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...
}
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...
You can look up the documentation for the "in_array" function here.
来源:https://stackoverflow.com/questions/56839049/how-to-check-the-array-value-in-if-condition