tags:

views:

56

answers:

2
$arr = array('1st', '1st');

The above $arr has 2 items , I want to repeat $arr so that it's populated with 4 items

Is there a single call in PHP?

+4  A: 

array_fill function should help:

array array_fill ( int $start_index, int $num, mixed $value )

Fills an array with num entries of the value of the value parameter, keys starting at the start_index parameter.

In your case code will look like:

$arr = array_fill(0, 4, '1st');
Jonas
+6  A: 
$arr = array('1st', '1st');

$arr = array_merge($arr, $arr);
Bill Karwin
I think this is better because I don't need to bother with the indexes.
wamp
It depends on what do you want to express. If you have an array with some values and you want to double them - use array_merge, and if you want to have an array with predefined number of the same values, then use array_fill. In coding it's not all about you (what is easier for you) it's about easiness to read and understand the code.
Jonas