proctk Posted February 24, 2008 Share Posted February 24, 2008 Hi I have the below code that prints this, notice the duplicate entries. I tried using array_unique but it does not remove the entries, any ideas why $arrfName = array($childFname, $siblingFname); $UarrfName = array_unique($arrfName); print_r ($UarrfName); result Array ( [0] => Test [1] => Tina ) Array ( [0] => Test [1] => Tina ) Array ( [0] => Test [1] => Tina ) Array ( [0] => Dylan [1] => Tina ) Array ( [0] => Dylan [1] => Tina ) Array ( [0] => Dylan [1] => Tina ) Array ( [0] => Jessyka [1] => Tina ) Array ( [0] => Jessyka [1] => Tina ) Array ( [0] => Jessyka [1] => Tina ) Array ( [0] => Tyler [1] => Tina ) Array ( [0] => Tyler [1] => Tina ) Array ( [0] => Tyler [1] => Tina ) Array ( [0] => Test [1] => Jackie ) Array ( [0] => Test [1] => Jackie ) Array ( [0] => Test [1] => Jackie ) Array ( [0] => Dylan [1] => Jackie ) Array ( [0] => Dylan [1] => Jackie ) Array ( [0] => Dylan [1] => Jackie ) Array ( [0] => Jessyka [1] => Jackie ) Array ( [0] => Jessyka [1] => Jackie ) Array ( [0] => Jessyka [1] => Jackie ) Array ( [0] => Tyler [1] => Jackie ) Array ( [0] => Tyler [1] => Jackie ) Array ( [0] => Tyler [1] => Jackie ) Quote Link to comment Share on other sites More sharing options...
Bauer418 Posted February 24, 2008 Share Posted February 24, 2008 Hi I have the below code that prints this, notice the duplicate entries. I tried using array_unique but it does not remove the entries, any ideas why $arrfName = array($childFname, $siblingFname); $UarrfName = array_unique($arrfName); print_r ($UarrfName); result Array ( [0] => Test [1] => Tina ) Array ( [0] => Test [1] => Tina ) Array ( [0] => Test [1] => Tina ) Array ( [0] => Dylan [1] => Tina ) Array ( [0] => Dylan [1] => Tina ) Array ( [0] => Dylan [1] => Tina ) Array ( [0] => Jessyka [1] => Tina ) Array ( [0] => Jessyka [1] => Tina ) Array ( [0] => Jessyka [1] => Tina ) Array ( [0] => Tyler [1] => Tina ) Array ( [0] => Tyler [1] => Tina ) Array ( [0] => Tyler [1] => Tina ) Array ( [0] => Test [1] => Jackie ) Array ( [0] => Test [1] => Jackie ) Array ( [0] => Test [1] => Jackie ) Array ( [0] => Dylan [1] => Jackie ) Array ( [0] => Dylan [1] => Jackie ) Array ( [0] => Dylan [1] => Jackie ) Array ( [0] => Jessyka [1] => Jackie ) Array ( [0] => Jessyka [1] => Jackie ) Array ( [0] => Jessyka [1] => Jackie ) Array ( [0] => Tyler [1] => Jackie ) Array ( [0] => Tyler [1] => Jackie ) Array ( [0] => Tyler [1] => Jackie ) Could I possibly see a bit more of your code? This must be being used in a loop. Quote Link to comment Share on other sites More sharing options...
Orio Posted February 24, 2008 Share Posted February 24, 2008 That's because your array is an array of arrays. You can't directly compare arrays (you cant do (arr1 == arr2)), so each element in your array is different. Orio. Quote Link to comment Share on other sites More sharing options...
proctk Posted February 24, 2008 Author Share Posted February 24, 2008 Thanks for the post. I took a different approach, getting close I continue to work through this. I was able to solve the duplication problem but its still not committing the ids when found. here is my new code while( $CheckMembers=mysql_fetch_array($qryCheckMembers)) : $childFname[$CheckMembers['childlastname']] = $CheckMembers['childfirstname']; /*$childLname[] = $CheckMembers['childlastname'];*/ $childDOB[] = $CheckMembers['childdob']; // $siblingFname[$CheckMembers['siblinglastname']] = $CheckMembers['siblingfirstname']; /*$siblingLname[] = $CheckMembers['siblinglastname'];*/ $siblingDOB[] = $CheckMembers['siblingdob']; // $parentFname[$CheckMembers['parentlastname']] = $CheckMembers['parentfirstname']; /*$parentLname[] = $CheckMembers['parentlastname'];*/ $parentDOB[] = $CheckMembers['parentdob']; $spouseFname[$CheckMembers['spouselastname']] = $CheckMembers['spousefirstname']; /*$spouseLname[] = $CheckMembers['spouselastname'];*/ $spousedob[] = $CheckMembers['spousedob']; $arrfName = array_merge( $childFname, $siblingFname, $parentFname, $spouseFname); $arrDOB = array_merge($childDOB,$siblingDOB, $parentDOB, $spousedob); $UarrDOB = array_unique($arrDOB); $UarrfName = array_unique($arrfName); endwhile; /*print_r ($UarrfName); print_r ($UarrDOB);*/ foreach ($UarrfName as $lastname => $firstname): Foreach($UarrDOB as $dob): $sqlCheckMember = ("SELECT first_name, last_name, DOB, user_id FROM users WHERE first_name = '$firstname' AND last_name = '$lastname' AND DOB = '$dob'"); $qryCheckMember = mysql_query($sqlCheckMember)or die("$sqlCheckMember Query Error: ".mysql_error()); while($parent_find = mysql_fetch_array($qryCheckMember)): $Member_id[] = $parent_find['user_id']; endwhile; endforeach; endforeach; /*print_r ($Member_id);*/ foreach($Member_id as $foundMemberId): $sqlbuddy = "SELECT buddylink.buddy_id, buddylink.owner_id, users.user_id, users.DOB, users.first_name, users.last_name FROM buddylink LEFT JOIN users ON buddylink.buddy_id = users.user_id WHERE buddylink.owner_id = '63' AND users.user_id != '$foundMemberId'"; echo $sqlbuddy; $qrybuddy = mysql_query($sqlbuddy)or die("SQL Error: $sql<br>" . mysql_error()); endforeach; out put all the values filling in the variable are correct but its still returning results that should not be SELECT buddylink.buddy_id, buddylink.owner_id, users.user_id, users.DOB, users.first_name, users.last_name FROM buddylink LEFT JOIN users ON buddylink.buddy_id = users.user_id WHERE buddylink.owner_id = '63' AND users.user_id != '100'SELECT buddylink.buddy_id, buddylink.owner_id, users.user_id, users.DOB, users.first_name, users.last_name FROM buddylink LEFT JOIN users ON buddylink.buddy_id = users.user_id WHERE buddylink.owner_id = '63' AND users.user_id != '13'SELECT buddylink.buddy_id, buddylink.owner_id, users.user_id, users.DOB, users.first_name, users.last_name FROM buddylink LEFT JOIN users ON buddylink.buddy_id = users.user_id WHERE buddylink.owner_id = '63' AND users.user_id != '4' Quote Link to comment Share on other sites More sharing options...
creekriot Posted August 6, 2009 Share Posted August 6, 2009 I see that this is a very old post, but will add my 2-cents worth anyway... After exploding an array with a 'comma' delimiter, array_unique failed to remove duplicates. I don't know why it works, but I finally discovered that by using 'sort' immediately after 'array_unique' all is well... $string = "a1,b1,b2,c2,b2,a2,a2,a2,a2,a2,a2,b2,y6,b2,b2,b2,c2,c2,c2,e3,t5"; $fullarray = explode(",", $string); $uniq = array_unique($fullarray); sort($uniq); // and to verify: for($x=0; $x<sizeof($uniq); $x++) { echo "<br>" . $uniq[$x]; } Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted August 6, 2009 Share Posted August 6, 2009 array_unique() doesn't reset the array keys, so your method of verification (which is dependent on sequential numeric keys starting from 0) won't always work.... it'll leave gaps, or miss out some of the later entries in the list. sort() resets the keys. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.