简体   繁体   English

如果var == array然后

[英]if var == array then

I have an array with ids 我有一个ID数组

$audit_answer_ids = array(85, 86);

now I have a foreach 现在我有一个foreach

$filtered_audits = array();
    foreach ($audits as $audit) {
      if (condition) {
        # code...
      }
      $filtered_audits[] = $audit;
    }

in the if (condition) I need to be able to do 在if(条件)中,我需要能够

$audit['Adusitoria']['id'] != $audit_answer_ids

so that way the system checks if $audit['Adusitoria']['id'] is equal to any of the ids in the array. 这样系统会检查$audit['Adusitoria']['id']是否等于数组中的任何ID。 Will just a simple if do? 如果简单的话会做吗?

Use the in_array function: 使用in_array函数:

if( !in_array( $audit['Adusitoria']['id'], $audit_answer_ids )) {
}

I assume you already have $audit['Adusitoria']['id'] variable stored. 我假设您已经存储了$audit['Adusitoria']['id']变量。
i was thinking to loop inside the array and then comparing 我想在数组内循环然后比较

Code

$audit_answer_ids = array(85, 86);

foreach ($audit_answer_ids as $data) {
    if ($audit['Adusitoria']['id'] != $data) {
        //do something
    } else {
        //do something else
    }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM