tags:

views:

211

answers:

2

Hi, i am having a action in the cakephp controller as which takes the report_id as an argument and the Selected Fields as an array .

i am trying to compare the array that contains the already existing attribute ids with the array what i have received from the action post .. if that particular attribute id if not present in the received array then i am trying to delete that entry in the Report s table.. i am not aware of how to use the NOt equal to operator in this scenario . Help me please.......

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 "     ";
      }
     }
    }
}
A: 

to look through an array to see if something is there, you need to do the following:

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";
    }
}
Marius
+1  A: 

Sounds like you're looking for array_intersect() and/or 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. ';
}

prints

201 not in array $comma_separated.
VolkerK