Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6129205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:42:54+00:00 2026-05-23T16:42:54+00:00

I have code already that loops through each member of my website and can

  • 0

I have code already that loops through each member of my website and can retrieve a particular variable (in this case it is $duration).

What I am trying to do is find people with the 10 lowest durations, and put them into an ordered array or list and then loop through the 10 and echo it out.

As a use case, let’s pretend that out of 100 members, 10 people have duration 10, 20, 30, 40, etc…, and we’ll pretend the other 90 members have durations somewhere between 101-1000.

As I loop through each member, I want to see if that member’s duration value is lower than any of the 10 I already have in some container (array?), and at the end be able to echo out the 10 from lowest to highest.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:42:55+00:00Added an answer on May 23, 2026 at 4:42 pm

    Since you have to loop through every value anyway, just add all of the values to an array and then use the sort method and take the first ten items (0-9). Here is the documentation on how exactly to use this method:

    http://www.php.net/manual/en/function.sort.php

    The benefit of this solution is that it will re-order the values so that key 0 corresponds with the lowest value. It also has the option to change what type of comparison you are doing (text, numeric, etc.)

    You could try to sort every time you find a new value (insert the new value and drop the highest value). That would save you the memory storage for the array but it will cost you in processing power when you do a comparison on each value. Even with optimization it probably won’t be good enough.

    If you really didn’t want to put every item in the array, you could create an array of 50 members (or whatever magic number you wanted to use). You could populate it with the first 50 durations and then sort it. Then you could add 40 more items to array positions 10-49 and sort it again. You could repeat that over and over, keeping your lowest ten items every time you add new durations. That way you could balance the performance hit of sorting every item with the memory hit of storing every item. You could dial it in by changing the array size.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have already have this code to force these URLs to HTTPS: RewriteCond %{HTTPS}
This is my php code (I already have a connection to the db): $result
This is similar to this question, except the code is already written. I have
I have code that looks like this: template<class T> class list { public: class
I have code that I want to look like this: List<Type> Os; ... foreach
I have some code, that simulate google images effect. How can I animate nested
I've got this code which basically loops through a set of folders and subfolders
So I have a file that already exists, and I read this file and
I already have code which lazy loads scripts on request. My issue now is
I'm working on a video codec for OMAP3430. I already have code written in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.