PHP Sorting

Hello. I have a multi dimensional array that needs to be sorted. Array top25 [index][product id][number_of_purchases]. The index will contain a unique number for each product id. First product will be 1, second product will be 2, etc. The product id will be a id number like 34324ac9a89. The number of purchases will be how many times the item has been purchased.

  • End result: I want to print the 25 most purchased products in this format.

product id - number of purchases

a39acz - 503
a8cz9c - 480
zc8ac - 392

How do I do this? Thanks

You have 3 dimensions, but what is the value of array element $top25[…][…][…] = product name? Normally when speaking of sortin an array, there are some values to be sorted, but you seem have only indexes. Maybe it is worth to revise your data structure and reduce dimensions to 2, and store number of purchases as array value $top25[…][…] = number of purchases. This way you can use functions array_multisort() and/or asort(). Otherwise you will have to have a several nested loops to re-build array to something sortable.

Sponsor our Newsletter | Privacy Policy | Terms of Service