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
My Answer:
, $start, $end) {
$array = array();
for ($i = 0; $i < $length; $i++) {
$row = array();
for ($j = 0; $j < $length; $j++) {
$row[] = rand($start, $end);
}
$array[] = $row;
}
return $array;
}
// Example usage
$multiArray = make(3, 1, 10);
print_r($multiArray); // Output the multidimensional array
This function will create a multidimensional array with the specified number of rows and columns, and fill it with random numbers within the specified range.
Rate this post
5 of 5 based on 2812 votesComments