citricsquid Posted January 1, 2010 Share Posted January 1, 2010 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 More sharing options...
rajivgonsalves Posted January 1, 2010 Share Posted January 1, 2010 $array[$id] in the above example would be of type array so basically its one, array keys have to be unique please elaborate on what your expected results you want to see, Its not very clear on what your trying to accomplish Link to comment https://forums.phpfreaks.com/topic/186844-help-with-arrays-and-array_unique/#findComment-986730 Share on other sites More sharing options...
phant0m Posted January 1, 2010 Share Posted January 1, 2010 I cannot understand your goal either. Assuming you have color date and name of ID 2. Do you only want to have one value instead of all three left? Link to comment https://forums.phpfreaks.com/topic/186844-help-with-arrays-and-array_unique/#findComment-986731 Share on other sites More sharing options...
citricsquid Posted January 1, 2010 Author Share Posted January 1, 2010 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? Link to comment https://forums.phpfreaks.com/topic/186844-help-with-arrays-and-array_unique/#findComment-986732 Share on other sites More sharing options...
rajivgonsalves Posted January 1, 2010 Share Posted January 1, 2010 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 Link to comment https://forums.phpfreaks.com/topic/186844-help-with-arrays-and-array_unique/#findComment-986736 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.