0

I have script like this

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;
}

the output like this

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

my question is, how to remove this duplicates array?

Thank you

1
  • Do you mean to add new elements as you go through the loop or replace $arr? What exactly are you trying to do? Commented May 15, 2013 at 8:30

2 Answers 2

3

You are overwriting $arr for each iteration.

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);
Sign up to request clarification or add additional context in comments.

1 Comment

yes you are right, I do overwriting $arr, thanks for remind me :)
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.