Search code examples
phparraysrepeat

Populate a new array with the same element repeated N times


Basically I need to create this array (given x = 3)

array('?','?','?');

I could do

for ($i = 0; $i < 3; $i++)
    $arr[] = '?';

But it's not so elegant. Is there any other way?


Solution

  • Use array_fill( start_index, num, value ):

    $arr = array_fill(0, 3, '?');