- PHP

Generate array with n elements in PHP

1. Use the array_fill() function.

$myArray = array_fill($min, $max, $value);

Example:

<?php

$min = 1;
$max = 10;

$myArray = array_fill($min, $max, null);
print_r($myArray);

?>

2. Make a loop.

Example:

<?php

$min = 1;
$max = 10;

for ($i=$min; $i <= $max; $i++) { 
    $myArray[$i] = 'value';
}

print_r($myArray);

?>