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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:11:16+00:00 2026-05-26T19:11:16+00:00

I try to write a script and a problem. Can you let me know

  • 0

I try to write a script and a problem. Can you let me know if you know how i can do this or ask someone if they know how can this be possibe.

  1. Max numbers which can be selected 1 to 20 numbers. it can loop and select any number between 1-20
  2. ignore these numbers e.g. 1,2,4,6,9,12 this will be array which can change.
  3. each array line can have upto 4 numbers
  4. Each array line needs to be unique
    5.I need to have around 10 arrays unique
  5. Max 2 numbers can match previous numbers see below.

How can i go about doing this. Any help would be great.

e.g.


Array(
    [0] => Array
    (
        [0] => 3
        [1] => 16
        [2] => 22
        [3] => 24
    )
    [1] => Array
    (
        [0] => 3
        [1] => 16
        [2] => 7
        [3] => 13
    )
    [2] => Array
    (
        [0] => 20
        [1] => 17
        [2] => 10
        [3] => 18
    )
)


This not allow as some array match each other



Array(
    [0] => Array
    (
        [0] => 3
        [1] => 16
        [2] => 22
        [3] => 24
    )
    [1] => Array - cant have this as 3 of the numbers matchs the previous array.only two numbers can match.
    (
        [0] => 3
        [1] => 16
        [2] => 22
        [3] => 13
    )
    [2] => Array
    (
        [0] => 20
        [1] => 17
        [2] => 10
        [3] => 18
    )
)

Thank you.

  • 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-26T19:11:17+00:00Added an answer on May 26, 2026 at 7:11 pm

    This seems to satisfy your conditions: http://codepad.viper-7.com/WHkQeD

    <?php
    $num_arrays = 10; $num_elements = 4;
    $min = 1; $max = 20;
    $exclude_numbers = array( 1, 4, 6); // Add numbers here to exclude
    
    $answer = array();
    for( $i = 0; $i < $num_arrays; $i++)
    {
        $answer[$i] = array();
        for( $j = 0; $j < $num_elements; $j++)
        {
            do
            {
                $current = rand( $min, $max);
                // If the previous array exists and there are more than two common elements when we add the $current element, continue
                if( isset( $answer[$i-1]) && count( array_intersect( $answer[$i-1], array_merge( $answer[$i], array( $current))) > 2)
                {
                    continue;
                }
            } while( in_array( $current, $exclude_numbers) || in_array( $current, $answer[$i]));
            $answer[$i][$j] = $current;
        }
    }
    
    var_dump( $answer);
    

    Edit: Here is a complete solution that satisfies all of your criteria.

    Demo

    <?php
    $num_arrays = 10; $num_elements = 4;
    $min = 1; $max = 20;
    $exclude_numbers = array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
    
    $answer = array();
    for( $i = 0; $i < $num_arrays; $i++)
    {
        $answer[$i] = array();
        for( $j = 0; $j < $num_elements; $j++)
        {
            do
            {
                // Get a random element
                $current = rand( $min, $max);
                $new_array = array_merge( $answer[$i], array( $current));
    
                // If the previous array has more than two common elements (because of the added $current), get a new $current
                if( isset( $answer[$i-1]) && count( array_intersect( $answer[$i-1], $new_array)) > 2)
                {
                    $answer[$i] = array_diff( $new_array, $answer[$i-1]);
                    $j = count( $answer[$i]) - 1;
                    continue;
                }
            } while( in_array( $current, $exclude_numbers) || in_array( $current, $answer[$i]));
    
            $answer[$i][$j] = $current; 
    
            // If the array is complete, we need to check for unique arrays
            if( count( $answer[$i]) == $num_elements)
            {
                $k = $i - 1;
                while( $k >= 0)
                {
                    if( count( array_diff( $answer[$k],  $answer[$i])) == 0)
                    {
                        // This array is the same as a previous one, start over
                        $answer[$i] = array();
                        $j = -1;
                        break;
                    }
                    $k--;
                }
                // Optionally sort each array
                sort( $answer[$i]);
            }
        }
    }
    
    var_dump( $answer);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to write KSH script for processing a file consisting of name-value pairs,
I am try to write some validation script using javascript and prototype. What I
I try to write a simple user script to enlarge the picture when you
I try to write a site with CodeIgniter but I've a problem with PHP.
I am try to write a php script that return the device from the
I'm trying to write a perl script that determines which users are currently logged
I'm having this problem and I reached a deadlock, I would try anything I've
I try to write to a large file, but it seems like it does
I try to write a simple client/server application (all application is a bluetooth service
I try to write a simple Markdown parser in JavaScript. Therefore I want to

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.