2009-09-30 48 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.