This question is intended as a reference for questions about sorting arrays in PHP. It is easy to think that your particular case is unique and worthy of a new question, but most are actually minor variations of one of the solutions on this page.

If your question is closed as a duplicate of this one, please ask for your question to be reopened only if you can explain why it differs markedly from all of the below.

How do I sort an array in PHP?
How do I sort a complex array in PHP?
How do I sort an array of objects in PHP?


  1. Basic one-dimensional arrays; Incl. Multidimensional arrays, incl. arrays of objects; Incl. Sorting one array based on another

  2. Sorting with SPL

  3. Stable sort

For the practical answer using PHP’s existing functions see 1., for the academic in-detail answer on sorting algorithms (which PHP’s functions implement and which you may need for really, really complex cases), see 2.

13 Answers
13

Leave a Reply

Your email address will not be published. Required fields are marked *