1

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?

kpp
  • 800
  • 2
  • 11
  • 27
  • 1
    `SELECT DISTINCT var1, var2, var3 FROM t` – Alma Do Jun 05 '14 at 11:04
  • 1
    Check this out http://stackoverflow.com/questions/2426557/array-unique-for-objects – CrazySabbath Jun 05 '14 at 11:08
  • @CrazySabbath that is pretty brilliant indeed. Just add all my variables to `__tostring()` – kpp Jun 05 '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 05 '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 05 '14 at 11:28

1 Answers1

0

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);
    }
}
Droga Mleczna
  • 511
  • 3
  • 7