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

Can you show us what's in your array? Maybe a print_r() or a var_dump() of it? How you're doing it now looks like it's a multidimensional array, but it's hard for us to judge what you're doing wrong if we don't know the data you're trying to manipulate :)

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

Try $cleanhr[0][0], $cleanhr[0][1], $cleanhr[0][2], etc.  Based on your array dump, it should work.

 

EDIT: Okay yeah, that DID post strange then.  Try what I just said, but now I'm thinking that it won't work.  Give it a shot anyway.

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

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).  

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.