I have a large array (a list of posts from VK, obtained through the API), in which the date keys containing the UNIX date are scattered. I need to sort an array by increasing the date value. How to do it?

here is the array output via print_r

  • Sort by key descending php.net/manual/ru/function.krsort.php - Visman
  • Add a sample dataset example - Umed
  • Hmm, how can there be identical keys in an array of two different elements? O_o - Visman
  • one
    Show the array / part of the array in question as Umed requested. And that fortune telling on the coffee grounds only. - Visman
  • one
    All code related to questions should be in the question itself . Links can only serve as a supplement - Dmitriy Simushev

1 answer 1

We must use usort :

 function cmp($a,$b) { return ($a['date'] > $b['date']) ? 1 : 0; } $arr = ...; //ответ от вк $arr = array_shift($arr); usort($arr,"cmp"); 

The usort function usort to pass its own function of comparing elements, in our case cmp .

UPD: read sort descending , corrected

  • Something at me it generally somehow randomly sorts. - devtwo 2:21 pm
  • @DeFF, it sorted in descending order, I somehow did not understand the post right away. Corrected, now sorts in ascending order. - Umed
  • @DeFF, If you throw off an example of your list of posts, then I will correct for it, but it is not clear what we are working with) - Umed
  • @DeFF, try this - Umed