Skip to content
Advertisement

php array unique and pushing data

i got data return from mongodb and as i searched i saw that i cannot user cursor twice so i put all data in arrays and now i need to split the data to unique name, the data i got :

JavaScript

what i need to accomplish is to get unique names and push Data return for each name like that:

JavaScript

i can get the unique Name’s but not pushing the data value to each name

i tried with two foreach loops without success , thanks for any help

JavaScript

Advertisement

Answer

A simple foreach loop can be used to create a new array. If you use the 'name' as the key then this makes it easy to remove the duplicates. All you have to do is check if you saw this name before and if not create a new entry in the new array and if you saw this name before just add a data item to the data array.

JavaScript

RESULT

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement