Something like
$main = [];
$number = 5;
foreach(range(1, $number) as $i) {
$main[] = [ 'value' => '', 'type' => 'col'];
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello everyone, I would like to do something like this:
$main = []
$number = 5;
[ 'value' => '', 'type' => 'col'] * $number
I want the result to be something like this:
$main = [
0 => [ 'value' => '', 'type' => 'col'],
1 => [ 'value' => '', 'type' => 'col'],
2 => [ 'value' => '', 'type' => 'col'],
3 => [ 'value' => '', 'type' => 'col'],
4 => [ 'value' => '', 'type' => 'col'],
]
The size of new $main array would be depending on the variable $number, which changes.
Is there a way to do this without pushing to the $main array in a loop?
@Abdulsalam How about this then
$number = 5;
$main = array_fill(0, $number, [ 'value' => '', 'type' => 'col']);
Please or to participate in this conversation.