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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:12:57+00:00 2026-06-17T14:12:57+00:00

In my php script, I have a variable $data that contains an array that

  • 0

In my php script, I have a variable $data that contains an array that may have various number of elements.

The script picks one of the array elements at random and outputs it to the browser:

# count number of elements in $data
$n = count($data);

# pick a random number out of the number of elements in $data
$rand = rand(0, ($n - 1));

# output a random element
echo '<p> . trim($data[$rand]) . '</p>';

QUESTION: I want to improve that script, so that it doesn’t output the same array element again until it runs out of array elements. For example, if an array contains elements numbered 0 to 9, and the script picked an array element #4, I want it to remember that, and next time the script runs, to exclude the element that was #4.

It could probably be done in many different ways, but I’m looking for the simplest and most elegant solution, and would be grateful for help from a PHP expert.

  • 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-06-17T14:12:58+00:00Added an answer on June 17, 2026 at 2:12 pm

    Save the numbers that were already picked in the user’s session.

    session_start();
    $n = count($data); 
    
    // If the array isn't initialized, or we used all the numbers, reset the array
    if( !isset( $_SESSION['used_nums']) || count( $_SESSION['used_nums']) == $n) {
        $_SESSION['used_nums'] = array();
    }
    
    do{ 
        $rand = rand(0, ($n - 1));
    } while( isset( $_SESSION['used_nums'][$rand]));
    
    echo '<p>' . trim($data[$rand]) . '</p>';    
    $_SESSION['used_nums'][$rand] = 1;
    

    Or, perhaps a more clever way, using array_intersect_key and array_rand:

    session_start();
    $n = count($data); 
    
    // If the array isn't initialized, or we used all the numbers, reset the array
    if( !isset( $_SESSION['used_nums']) || count( $_SESSION['used_nums']) == $n) {
        $_SESSION['used_nums'] = array();
    }
    
    $unused = array_intersect_key( $data, $_SESSION['used_nums'];
    $rand = array_rand( $unused);
    
    echo '<p>' . trim($unused[$rand]) . '</p>';    
    $_SESSION['used_nums'][$rand] = 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php variable called $usershours that contains an array, and I want
I have this small PHP script that takes a POST variable and writes it
I have a php script that inserts data from an Android app into a
If I have a variable that needs to be posted to a PHP script
I have a form that posts some data to a PHP processing script. I
I have a PHP script that breaks if a variable is not populated and
I have an iphone app that sends and retrieves data from a php script.
I have an Apache server running a PHP script that receives data via a
I have a script that posts data to a php script: xhr.open(POST,processData.php); And i
I have php script that creates a temporary watermark image for users that are

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.