13 10, 2010

array_fill

By | 2010-10-13T11:17:44+00:00 October 13th, 2010|Categories: PHP, Ruby|Tags: , , , , , |1 Comment

The array_fill() function in PHP allows an array to be populated (i.e. filled) with a particular value.

PHP

$a = array_fill(3, 5, 'php');
print_r($a);
/*
Array (
	[3] => php
	[4] => php
	[5] => php
	[6] => php
	[7] => php
)
*/

In Ruby this is not really possible because Ruby arrays must have their keys filled in the correct order, i.e. you cant skip assigning values to keys in a Ruby array. As such, the next best thing is to fill those values with nil, or use a hash instead.

Ruby

a = [nil] * 3 + ['php'] * 5;
puts a;
# => [nil, nil, nil, 'php', 'php', 'php', 'php', 'php']

This Is A Custom Widget

This Sliding Bar can be switched on or off in theme options, and can take any widget you throw at it or even fill it with your custom HTML Code. Its perfect for grabbing the attention of your viewers. Choose between 1, 2, 3 or 4 columns, set the background color, widget divider color, activate transparency, a top border or fully disable it on desktop and mobile.

This Is A Custom Widget

This Sliding Bar can be switched on or off in theme options, and can take any widget you throw at it or even fill it with your custom HTML Code. Its perfect for grabbing the attention of your viewers. Choose between 1, 2, 3 or 4 columns, set the background color, widget divider color, activate transparency, a top border or fully disable it on desktop and mobile.