i’ve been wondering what’s the quickest way to fill an array with a constant value? sometimes the code is necessary. for example, i have X number of items in an array and i wish to put them evenly into an table. to call another function, i need to pass in an array of values and an array of table width. well in my case, i just need to fill an array for table width with something like
$table_width = array('50%', '50%');
here’s my code to fill an array assuming $num_cols contains the number X.
$dummy = range(0, $num_cols - 1); foreach($dummy as $val) { $width_per[$val] = round(100/$num_cols)."%"; }
Is there a faster way to do this?
array_fill 🙂