0

我有这样的脚本

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 )

我的问题是,如何删除这个重复数组?

谢谢

4

2 回答 2

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);
于 2013-05-15T08:28:13.550 回答
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
于 2015-08-03T09:27:37.187 回答