2009-09-30 62 views
0

我在cakephp控制器中执行一个动作 ,它将report_id作为参数,将选定的字段作为数组使用。如何在Cakephp中使用不相等的运算符

我想比较包含已经存在的属性ids的数组与数组我所收到的动作帖子..如果该特定的属性ID如果不存在于收到的数组然后我试图删除在报表中输入.. 我不知道如何在这种情况下使用NOt等于操作符。请帮我.......

function updateReport($report_id){ 
    $attribute_ids=$this->params['form']['attr']; 
    $comma_separated = explode(",", $attribute_ids); 
    $count=count($comma_separated); 

    //$comma_separated contains 200,203 
    $exists=$this->Report->find('all',array('conditions'=>array('Report.report_id'=>$report_id))); 
    //$exists contains the attributes as 200 , 201, 203 

    foreach($exists as $exist){ 
     for($i=0;$i<$count;$i++){ 
      if($exist['Report']['attribute_id']==$comma_separated[$i]){ 
       echo "not in array $comma_separated ".$exist['Report']['attribute_id'];echo "  "; 
      } 
     } 
    } 
} 

回答

0

通过一个数组来看看,看看什么是存在的,你需要做到以下几点:

foreach($exists as $exist){ 
    $exists = false; 
    for($i=0;$i<$count;$i++){ 
     if($exist['Report']['attribute_id']==$comma_separated[$i]){ 
      //It exists in the array; 
      $exists = true; 
     } 
    } 
    if($exists){ 
     echo "$exist['Report']['attribute_id'] exists"; 
    }else{ 
     echo "$exist['Report']['attribute_id'] does not exist"; 
    } 
} 
1

听起来你正在寻找array_intersect()和/或array_diff()

$comma_separated = array(200, 203); 
$exists=array(200, 201, 203); 

foreach(array_diff($exists, $comma_separated) as $x) { 
    echo $x, ' not in array $comma_separated. '; 
} 

打印

201 not in array $comma_separated.