Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

259
Views
How to sort array and take 5 items only?

How to get only 5 newest articles from array?

For example:

  usort($articles, function ($item1, $item2) {
        return $item2['created_at'] <=> $item1['created_at'];
    });

This will sort them descending and I don't need to show all of them.

What would be the best way to do this? Is it possible without looping again and storing in new array?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Using array_slice on the sorted array should do.

usort($articles, function ($item1, $item2) {
    return $item2['created_at'] <=> $item1['created_at'];
});

$fiveFirst = array_slice($articles, 0, 5);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!