Populating php array without specifying all elements when elements are identical

I want to create an array like this [1, 1, 1, 1, 1]

In python, I can use myarray = [1]*5

Is there an equivalent method in PHP or do I need to add all the elements of the loop.

+3


source to share


2 answers


http://php.net/manual/en/function.array-fill.php



<?php

var_dump( array_fill( 0, 5, 1 ) );

?>

      

+11


source


what you need is array_fill



see php array_fill

+1


source







All Articles