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 6832825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:54:20+00:00 2026-05-26T22:54:20+00:00

I have an array of records from a database (although the database is irrelevant

  • 0

I have an array of records from a database (although the database is irrelevant to this question — it eventually becomes an array of “rows”, each row is an array with string keys corresponding to the field name). For example:

$items = array(
    1 => array('id' => 1, 'name' => 'John', 'created' => '2011-08-14 8:47:39'),
    2 => array('id' => 2, 'name' => 'Mike', 'created' => '2011-08-30 16:00:12'),
    3 => array('id' => 5, 'name' => 'Jane', 'created' => '2011-09-12 2:30:00'),
    4 => array('id' => 7, 'name' => 'Mary', 'created' => '2011-09-14 1:18:40'),
    5 => array('id' => 16, 'name' => 'Steve', 'created' => '2011-09-14 3:10:30'),
    //etc...
);

What I want to do is shuffle this array, but somehow give more “weight” to items with a more recent “created” timestamp. The randomness does not have to be perfect, and the exact weight does not really matter to me. In other words, if there’s some fast and simple technique that kinda-sorta seems random to humans but isn’t mathematically random, I’m okay with that. Also, if this is not easy to do with an “infinite continuum” of timestamps, it would be fine with me to assign each record to a day or a week, and just do the weighting based on which day or week they’re in.

A relatively fast/efficient technique is preferable since this randomization will occur on every page load of a certain page in my website (but if it’s not possible to do efficiently, I’m okay with running it periodically and caching the result).

  • 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-26T22:54:21+00:00Added an answer on May 26, 2026 at 10:54 pm

    After being partially inspired by the response from @Tadeck , I came up with a solution. It’s kind of long-winded, if anyone could simplify it that would be great. But it seems to work just fine:

    //Determine lowest and highest timestamps
    $first_item = array_slice($items, 0, 1);
    $first_item = $first_item[0];
    $min_ts = strtotime($first_item['created']);
    $max_ts = strtotime($first_item['created']);
    foreach ($items as $item) {
        $ts = strtotime($item['created']);
        if ($ts < $min_ts) {
            $min_ts = $ts;
        }
        if ($ts > $max_ts) {
            $max_ts = $ts;
        }
    }
    
    //bring down the min/max to more reasonable numbers
    $min_rand = 0;
    $max_rand = $max_ts - $min_ts;
    
    //Create an array of weighted random numbers for each item's timestamp
    $weighted_randoms = array();
    foreach ($items as $key => $item) {
        $random_value = mt_rand($min_rand, $max_rand); //use mt_rand for a higher max value (plain old rand() maxes out at 32,767)
        $ts = strtotime($item['created']);
        $ts = $ts - $min_ts; //bring this down just like we did with $min_rand and $max_rand
        $random_value = $random_value + $ts;
        $weighted_randoms[$key] = $random_value;
    }
    
    //Sort by our weighted random value (the array value), with highest first.
    arsort($weighted_randoms, SORT_NUMERIC);
    
    $randomized_items = array();
    foreach ($weighted_randomsas $item_key => $val) {
        $randomized_items[$item_key] = $items[$item_key];
    }
    
    print_r($randomized_items);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have an array of records retreived from a database. The array is
I have loaded an associative array of records from a MySQL database table. The
]i have this graph that shows the last 7 records from my sqlite database,
I have a program which fetches records from database (using Hibernate) and fills them
I have an SQLite database from which I need to delete records periodically. What
I have some data from database coming out in a loop. Each of these
I have a row of records from a 3rd party, including a 3rd party
I would like to have a custom function to insert/edit/delete records from a database
I have extracted records from a database and stored them on an HTML page
I have two arrays (records returned from a database query) that I'm merging. I

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.