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

  • Home
  • SEARCH
  • 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 6070157
In Process

The Archive Base Latest Questions

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

I want to select a random value from a array, but keep it unique

  • 0

I want to select a random value from a array, but keep it unique as long as possible.

For example if I’m selecting a value 4 times from a array of 4 elements, the selected value should be random, but different every time.

If I’m selecting it 10 times from the same array of 4 elements, then obviously some values will be duplicated.

I have this right now, but I still get duplicate values, even if the loop is running 4 times:

$arr = $arr_history = ('abc', 'def', 'xyz', 'qqq');

for($i = 1; $i < 5; $i++){
  if(empty($arr_history)) $arr_history = $arr; 
  $selected = $arr_history[array_rand($arr_history, 1)];  
  unset($arr_history[$selected]); 
  // do something with $selected here...
}
  • 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-23T09:54:06+00:00Added an answer on May 23, 2026 at 9:54 am

    You almost have it right. The problem was the unset($arr_history[$selected]); line. The value of $selected isn’t a key but in fact a value so the unset wouldn’t work.

    To keep it the same as what you have up there:

    <?php
    
    $arr = $arr_history = array('abc', 'def', 'xyz', 'qqq');
    
    for ( $i = 1; $i < 10; $i++ )
    {
      // If the history array is empty, re-populate it.
      if ( empty($arr_history) )
        $arr_history = $arr;
    
      // Select a random key.
      $key = array_rand($arr_history, 1);
    
      // Save the record in $selected.
      $selected = $arr_history[$key];
    
      // Remove the key/pair from the array.
      unset($arr_history[$key]);
    
      // Echo the selected value.
      echo $selected . PHP_EOL;
    }
    

    Or an example with a few less lines:

    <?php
    
    $arr = $arr_history = array('abc', 'def', 'xyz', 'qqq');
    
    for ( $i = 1; $i < 10; $i++ )
    {
      // If the history array is empty, re-populate it.
      if ( empty($arr_history) )
        $arr_history = $arr;
    
      // Randomize the array.
      array_rand($arr_history);
    
      // Select the last value from the array.
      $selected = array_pop($arr_history);
    
      // Echo the selected value.
      echo $selected . PHP_EOL;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With Perl's WWW::Mechanize module, I want to select a random value from a select
Hi all, My requirement is simple.I want to select random rows from a table.
I want to write a query like this: SELECT o.OrderId, MAX(o.NegotiatedPrice, o.SuggestedPrice) FROM Order
I want to do something like select * from tvfHello(@param) where @param in (Select
I want to select all rows of a table followed by a random number
I am getting random row from my table using the following query: SELECT value_id
I'm trying to select a random 10% sampling from a small table. I thought
I want to randomly choose a value from a small range of integers (less
I want to select a radio button from javascript. I am using this simple
SELECT id FROM films WHERE id IN (90,4,40) result 4 40 90 but 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.