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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:49:45+00:00 2026-05-31T04:49:45+00:00

I have an array with 18 values in it from which I select random

  • 0

I have an array with 18 values in it from which I select random values using $array[rand(0,17)]. I put these randomly selected values next to each other on the page. Within the array are 6 sets of values that I do not want to be put next to each other on the page. Is there any way that I can detect when the pairs are together and select new values because of that

  • 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-31T04:49:47+00:00Added an answer on May 31, 2026 at 4:49 am

    warning: Do you know for sure that you won’t get any degenerate cases where there are no possible orderings of the array? For example, if you won’t allow the pairs [1,2] or [2,1] and the array you get is [1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2], the you’re out of luck. There’s no way to display the array in the way you want, and a method like I describe below will never terminate.


    I would use shuffle($array) and then iterate through the shuffled array one item at a time, to find out whether any value is “incompatible” with the item before it. If so, just reshuffle the array and try again. You can’t predict how many tries it will take to get a shuffled array that works, but the amount of time it takes should be negligible.

    To detect whether two values are compatible, I’d suggest making an array that contains all incompatible pairs. For example, if you don’t want to have the consecutive pairs 1 and 3 or 2 and 5, then your array would be:

    $incompatible = array(
        array(1,3),
        array(2,5) );
    

    Then you’d iterate over your shuffled array with something like:

    for ($i=1; i<count($array)-1; i++;) {
        $pair = $array[i, i+1]; // this is why the for loop only goes to the next-to-last item
        if in_array($pair, $incompatible) { 
            // you had an incompatible pair in your shuffled array.
            // break out of the for loop, re-sort your array, and try again.
        }
    } 
    // if you get here, there were no incompatible pairs
    // so go ahead and print the shuffled array!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array with integers of values from 0 to 100. I wish
I have an array of values that is passed to my function from a
I have an int array containing gray scale values from 0-254, i also have
I have a ParameterInfo array. I need to remove certain values from the array.
I have an array of values which is almost, but not quite sorted, with
Suppose I have an array of values [a,b,c,d,...] and a function f(x,...) which returns
I have a experiment which displays the random images from the database and the
I have an array which populated by a cookie. The length differ each time.
I have an array char[] Select = {'A','B','C','D','E','F','G','H','I','J'} and each element in this array
I have an array of values all well within the range 0 - 63,

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.