重复数组到一定长度?

Tei*_*eiv 8 php arrays repeat

我有一个数组,例如4个元素array("a", "b", "c", d");,重复这个数组的最快方法是创建一个具有一定长度的新数组,例如71个元素?

2nd*_*boy 9

// the variables
$array = array("a", "b", "c", "d");
$desiredLength = 71;
$newArray = array();
// create a new array with AT LEAST the desired number of elements by joining the array at the end of the new array
while(count($newArray) <= $desiredLength){
    $newArray = array_merge($newArray, $array);
}
// reduce the new array to the desired length (as there might be too many elements in the new array
$array = array_slice($newArray, 0, $desiredLength);
Run Code Online (Sandbox Code Playgroud)