Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Before you go all duplicate of "question" on me. Please read out the situation.

In my application I build objects based on data from the database. However sometimes I happens that the same database entry is read twice due to a LIKE statement in my sql query(which runs once for each user in the group of the current user). Due to this it can happen that multiple equal objects are created which will not be considered duplicates according to array_unique.

//These objects are equal but are not duplicates.
//And these cases need to be removed from the array.
$z = new Obj(1,2,3);
$y = new Obj(1,2,3);

//code example
$e = array();

while($row = mysqli_fetch_array($result)){ //result is result from query.
    $a = $row['var1'];        //obtains data from result
    $b = $row['var2'];
    $c = $row['var3'];
    $d = new Obj($a, $b, $c); //Creates new object
    array_push($e, $d);       //Holds all objects
}

class Obj{

    public $a;
    public $b;
    public $c;

    public function __construct($a, $b, $c){
        $this->a = $a;
        $this->b = $b;
        $this->c = $c;
    }
}

//This could work but it is a slow(forbidden) algorithm.
foreach($e as $f){
    foreach($e as $g){
        //REMOVE DUPLICATES
    }
}

//This wont work because technically the 2 duplicates are 2 different objects.
//And not duplicates right?
$e = array_unique($e)

So the question is: Is there a simple or faster way to remove any duplicates from this array instead of using a double loop?

share|improve this question
1  
SELECT DISTINCT var1, var2, var3 FROM t –  Alma Do Jun 5 '14 at 11:04
1  
    
@CrazySabbath that is pretty brilliant indeed. Just add all my variables to __tostring() –  kpp Jun 5 '14 at 11:10
    
@kpp, well, you didn't specify exactly what your object looks like, some var_dumps would help, and if your object has only 3 members then why not? However I'd aggree with Alma Do that if possible this should be solved at database level (Sql). [P.s was that sarcasm?;d] –  CrazySabbath Jun 5 '14 at 11:15
    
I wish I could solve it like Alma Do does it, but like I said the query gets run multiple times but for each different user, and at some point it has a LIKE statement which can cause a duplicate, because its possible that a result can belong to more than a single user. This is a rare occasion though. (essentially all results belong to all users in a group, but the groupID is not defined in this table and I am not allowed to change the table. So I am forced to filter all data with this massive query which can cause some duplicates. They need to be filtered for showing them to the user). –  kpp Jun 5 '14 at 11:28

1 Answer 1

up vote 0 down vote accepted

Maybe something like this:

$e = array();
$ids = array();

while($row = mysqli_fetch_array($result)){ //result is result from query.
    $a = $row['var1'];        //obtains data from result
    $b = $row['var2'];
    $c = $row['var3'];
    $d = new Obj($a, $b, $c); //Creates new object
    $id = base64_encode(serialize($d));

    if (! in_array($id, $ids)) {
        array_push($e, $d);
        array_push($ids, $id);
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.