How to repeat an array in PHP?

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

There are 2 elements in the above $arr , I want to repeat $arr so that it is filled with 4 items

Is there one call in PHP?

+4
source share
2 answers

array_fill should help:

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

Fills an array with numerical values โ€‹โ€‹of the value parameter values, keys starting with the parameter start_index.

In your case, the code will look like this:

 $arr = array_fill(0, 4, '1st'); 
+10
source
 $arr = array('1st', '1st'); $arr = array_merge($arr, $arr); 
+7
source

Source: https://habr.com/ru/post/1311702/


All Articles