geeky · php code

quickly fill an array

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?

One thought on “quickly fill an array

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s