I want to run through a data-set and see if there are any duplicates in it. I am wondering which way would be more efficient for the server. Running through the data-set and sending new queries to the database, or running through the data-set with a second nested for loop.
I think the code example will make it more understandable.
SELECT * FROM contacts;
Let's say this query yields an array:
[0] {id: 1, firstname: 'John', lastname: 'Smith'}
[1] {id: 2, firstname: 'Michael', lastname:'Jones'}
...
[99] {id: 100, firstname: 'Jerry', lastname:'Brown'}
And is saved into a php array:
$data
Now let's say that I want to run through each contact in $data
and then see if there are any contacts with the same first name and last name.
Which method would be more efficient?
1:
for($i = 0 ; $i < sizeof($data) ; $i++){
#query db:
$newQuery = SELECT * FROM Contacts WHERE firstname=$data[$i]['firstname'] AND lastname=$data[$i]['lastname'];
if(sizeof($newQuery > 1)){
#log contacts.
}
}
2:
for($i = 0 ; $i < sizeof($data) ; $i++){
for($j = $i+1; $j < sizeof($data); $i++){
if($data[$i]['firstname'] === $data[$j]['firstname'] && $data[$i]['lastname'] === $data[$j]['lastname']){
#log contacts;
}
}
}
Of course if i'm going about this totally wrong and there's a better way to do it all together I would be happy to learn!