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?
SELECT DISTINCT var1, var2, var3 FROM t
– Alma Do Jun 5 '14 at 11:04__tostring()
– kpp Jun 5 '14 at 11:10LIKE
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