mardi 8 mars 2022

How to write multidimensinal array php function?

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




Aucun commentaire:

Enregistrer un commentaire