Jump to content

Help with arrays and array_unique


citricsquid

Recommended Posts

I have some data that I'm putting into an array, the data is as follows: non unique ID (can occur multiple times), name, description and colour. Now, once I've compiled all the data I want to strip out the duplicates by ID, so if I have 3 with the ID of 8, I only want to display 1 of these.

 

$id = 1;
$name = "name";
$description = "description";
$color = "colour";

$array[$id]["name"] = $name;
$array[$id]["description"] = $description;
$array[$id]["color"] = $color;

 

However if I try and do array_unique (assuming I have multiple $id) it doesn't work. Is there anything I should know? Must an array key be unique? If so; how can I use the id as the thing for array_unique to try against? I've read the manual multiple times and I can't work it out.

 

Thanks :3

Link to comment
https://forums.phpfreaks.com/topic/186844-help-with-arrays-and-array_unique/
Share on other sites

Apologies, I'm not the best at explaining.

 

I have some data, an example of the data is as follows:

 

ID | Type | Age | Name

 

01 | CAT | 12 | JOHN
02 | DOG | 13 | EDWARD
03 | HORSE | 02 | MARY
01 | CAT | 12 | JOHN

 

I want to stick this data into an array and then I want to strip out the duplicates by ID, so if the ID is the same as another I want to remove it from the array. However the key (?) cannot be the same as another and I only *think* array_unique supports removing by duplicate key.

 

Does this make sense?

if your adding your data to the array the way you showed earlier you should not have problems with duplicate data only one id would be shown the last one, however if there are multiple records of the id with different datasets your logic would fail, how are you reading the data into your script, I don't see it in your post earlier

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.