1

I need to make a function which creates multidimensional array, with adjustable number of rows and columns, and generated random numbers of each row, also adjustable

I have this attempt now, but it seems to go into an infinite loop:

function make($length,$start,$end)
{
    if ($length<abs($start-$end)+1){
        for ($i=0; $i < $length; $i++){
        while (!isset($array) || count($array)<$length){
            $vel=rand($start,$end);
            if (!isset($array) || !in_array($vel,$array)){
                $array[$i][]=$vel;
            }
        }
        }
    
            return $array;
            } else {
                return false;
            }
        }

Please help, I can`t seem to figure it out

4
  • 2
    Start with Good code indentation it would help us read the code and more importantly it will help you debug your code Take a quick look at a coding standard for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. Commented Mar 8, 2022 at 9:40
  • Please show us the function call yo are using, or a few Commented Mar 8, 2022 at 9:52
  • What do you actually want the resulting array to look like Commented Mar 8, 2022 at 9:57
  • You know, code is a lot easier to read and understand if you leave spaces between the various parts. See how I've edited what you've posted. As RiggsFolly indicated above, it wil make your life easier if you use clear formatting. It will certainly make easier the lives of anyone else who has to read your code, such as us! Commented Mar 8, 2022 at 10:10

1 Answer 1

1

You were not checking the right array parts in the loops

function make($length,$start,$end)
{
    if ($length<abs($start-$end)+1){
        for ($i=0; $i < $length; $i++){
            while (!isset($array[$i]) || count($array[$i])<$length){
//                              ^^^^                 ^^^^
                $vel=rand($start,$end);
                if (!isset($array[$i]) || !in_array($vel,$array[$i])){
//                               ^^^^                          ^^^^
                    $array[$i][]=$vel;
                }
            }
        }
    
        return $array;
    } else {
        return false;
    }
}

print_r( make(5,10,30) );

The RESULT

Array
(
    [0] => Array
        (
            [0] => 30
            [1] => 16
            [2] => 27
            [3] => 17
            [4] => 26
        )

    [1] => Array
        (
            [0] => 21
            [1] => 13
            [2] => 19
            [3] => 25
            [4] => 12
        )

    [2] => Array
        (
            [0] => 12
            [1] => 28
            [2] => 20
            [3] => 19
            [4] => 27
        )

    [3] => Array
        (
            [0] => 23
            [1] => 17
            [2] => 12
            [3] => 16
            [4] => 15
        )

    [4] => Array (
            [0] => 17
            [1] => 11
            [2] => 22
            [3] => 13
            [4] => 10
        )
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.