Jump to content

array_unique deletes all but first value


cfgcjm

Recommended Posts

I've passed data into an array (and i've triple checked to make sure it is in there) and i'm trying to remove the duplicates. The echo is "5-A******" which is incorrect. It should be "5-A*6-B*7-A*7-B*8-A"

 

Diagnosis welcome...

 

My code is as follows:

 

sort($homerooms);
$cleanhr=array_unique($homerooms);

echo $cleanhr[0][0];
echo"*";
echo $cleanhr[1][0];
echo"*";
echo $cleanhr[2][0];
echo"*";
echo $cleanhr[3][0];
echo"*";
echo $cleanhr[4][0];
echo"*";
echo $cleanhr[5][0];
echo"*";
echo $cleanhr[6][0];

Link to comment
Share on other sites

Array ( [0] => Array ( [0] => 5-A ) [1] => Array ( [0] => 6-B ) [2] => Array ( [0] => 6-B ) [3] => Array ( [0] => 7-A ) [4] => Array ( [0] => 7-B ) [5] => Array ( [0] => 8-A ) )

 

 

and you're right it is coded as multi-dimensional however only one dimension is being used

Link to comment
Share on other sites

Changed it to

 

sort($homerooms[0]);
$cleanhr=array_unique($homerooms[0]);

echo $cleanhr[0][0];
echo"*";
echo $cleanhr[1][0];
echo"*";
echo $cleanhr[2][0];
echo"*";
echo $cleanhr[3][0];
echo"*";
echo $cleanhr[4][0];
echo"*";
echo $cleanhr[5][0];
echo"*";
echo $cleanhr[6][0];

 

and it now gave me "6******"...odd

Link to comment
Share on other sites

That is because there wasn't a second dimension in the array after you set the variable equal to only one of the dimensions.

Umm, where did he do that? It didn't work because the way array_unique works is it removes duplicates inside a single level array, but the values were inside separate arrays (which is inside a master array).  

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.