2013-05-15 87 views
0

我有這樣刪除重複的數組PHP

foreach ($onerow as $onekey => $dt){ 
$arr = array(); 
foreach($row as $classkey => $classfoto): 
    $cek_class_foto = explode("/",$classfoto->name); 
    if($dt->foto_naam!=$cek_class_foto[1]){ 
    $arr = array($dt->foto_naam); 
    print_r(array_unique($arr)); 
    } 
endforeach; 
} 

輸出這樣

Array ([0] => _b101203.jpg) 
Array ([0] => _b101203.jpg) 

我的問題腳本,如何消除這種重複陣列?

謝謝

+0

你的意思是,你去通過循環或更換$ ARR添加新元素?你究竟想要做什麼? – Belinda

回答

3

您在每次迭代覆蓋$arr

foreach ($onerow as $onekey => $dt){ 
    $arr = array(); 
    foreach($row as $classkey => $classfoto): 
     $cek_class_foto = explode("/",$classfoto->name); 
     if($dt->foto_naam!=$cek_class_foto[1]){ 
      $arr[] =$dt->foto_naam; 
     } 
    endforeach; 
} 
$arr = array_unique($arr); 
+0

是的你是對的,我覆蓋'$ arr',感謝提醒我:) – ranggadablues

0
$array1 = Array 
      (
       '0' => Array 
        (
         'messageId' => 9, 
         'userId' => 47, 
         'petId' => 68, 
         'message' => 'how hello', 
         'senderId' => 48, 
         'senderPetId' => 66, 
         'messageCreateTime' => '2015-07-31 11:44:59' 
        ), 
       '1' => Array 
        (
         'messageId' => 8, 
         'userId' => 49, 
         'petId' => 69, 
         'message' => 'pppppppppp', 
         'senderId' => 48, 
         'senderPetId' => 67, 
         'messageCreateTime' => '2015-07-31 11:15:16' 
        ), 
       '2' => Array 
        (
         'messageId' => 6, 
         'userId' => 48, 
         'petId' => 67, 
         'message' => 'gggg', 
         'senderId' => 49, 
         'senderPetId' => 69, 
         'messageCreateTime' => '2015-07-31 11:13:42' 
        ),     
       '3' => Array 
        (
         'messageId' => 2, 
         'userId' => 48, 
         'petId' => 67, 
         'message' => 'aaaaaaaa', 
         'senderId' => 47, 
         'senderPetId' => 68, 
         'messageCreateTime' => '2015-07-31 11:15:33' 
        ) 

      ); 

      /* code for removing last duplicate array within result array by KS */ 
      /* matching between : userId, petId, senderId, senderPetId */ 
      $temp_array = array(); 
      $i = 0; 
      foreach($array1 as $key=>$value) 
      { 
       $FLAG = FALSE; 
       $temp_array = $array1[$key]; 
       if($i==0) 
       {  
        $final_array[] = $temp_array; 
       } 
       else 
       { 
        for($j=0;$j<count($final_array);$j++) 
        {   
         if(($final_array[$j]['userId']==$temp_array['userId'] && $final_array[$j]['petId']==$temp_array['petId'] && $final_array[$j]['senderId']==$temp_array['senderId'] && $final_array[$j]['senderPetId']==$temp_array['senderPetId']) || 
         ($final_array[$j]['userId']==$temp_array['senderId'] && $final_array[$j]['petId']==$temp_array['senderPetId'] && $final_array[$j]['senderId']==$temp_array['userId'] && $final_array[$j]['senderPetId']==$temp_array['petId'])) 
         {     
          $FLAG = TRUE;    
         }   
        }  
        if($FLAG == FALSE){ 
         $final_array[] = $temp_array; 
        } 
       } 
       $i++; 
      } 
      print('<pre>'); 
      print_r($final_array); 

    enter code here