Jump to content

array_unique not working


proctk

Recommended Posts

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 )

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'

Link to comment
Share on other sites

  • 1 year later...

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];

}

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.