Repeat array to specific length?

I have an array, for example, with 4 elements array("a", "b", "c", d"); what is the fastest way to repeat this array to create a new array with a specific length, for example 71 elements?

+6
arrays php repeat
source share
10 answers
 // 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); 
+7
source share

Solution using SPL InfiniteIterator :

 <?php function fillArray1($length, $values) { foreach (new InfiniteIterator(new ArrayIterator($values)) as $element) { if (!$length--) return $result; $result[] = $element; } return $result; } var_dump(fillArray(71, array('a', 'b', 'c', 'd'))); 

Real SPL hackers may have dropped if (!$length--) break; and instead used a limit iterator: new LimitIterator(new InfiniteIterator(new ArrayIterator($values)), 0, $length) , but I thought it would be overkill ...

+3
source share

To join this club:

 $result = call_user_func_array('array_merge', array_fill(0, ceil($size/count($array)), $array)); while(count($result) > $size) array_pop($result); 

You asked faster, so I did a test (Source: http://pastebin.com/G5w7QJPU )

 Kau-Boy: 5.40128803253 Frxstrem: 5.00970411301 NikiC: 4.12150001526 user2469998: 0.561513900757 Alexander: 1.92847204208 Hammerite: 2.17130494118 Max: 12.9516701698 Evert: 1.9378361702 Christoph: 1.6862449646 Test took 35.7696909904s 

user2469998 is the fastest, but it only works for string values ​​with single characters (or with the same length if you use the second str_split parameter).

+2
source share

A simple solution using each() and reset() and an internal array pointer:

 <?php $array = array('a', 'b', 'c', 'd'); $length = 71; $result = array(); while(count($result) < $length) { $current = each($array); if($current == false) { reset($array); continue; } $result[] = $current[1]; } echo count($result); // Output: 71 
+1
source share
 $newarray = array(); $i = 0; $oldarrayvalues = array_values($oldarray); $oldarraysize = count($oldarrayvalues); if ( $oldarraysize ) { while ( count($newarray) < DESIRED_ARRAY_SIZE ) { $newarray[] = $oldarrayvalues[$i]; $i++; $i %= $oldarraysize; } } 
0
source share
 <?php $array = array('a', 'b', 'c', 'd'); $end = 71; $new_array = array(); while(count($new_array) <= $end) { foreach($array as $key => $value) { $new_array[] = $value; } } $new_array = array_slice($new_array, 0, $end); 

Tested and working.

You can test yourself by adding this:

 echo '<pre>'; print_r($new_array); echo '</pre>'; 
0
source share

If you have PHP 5.3, you can also try the following:

 function fill(array $initalArray, $toCount) { $initialArrayCount = count($initalArray); $fillUp = function(array $filledUpArray, $missingCount) use(&$fillUp, $initalArray, $initialArrayCount, $toCount) { if($missingCount <= 0) return array_slice($filledUpArray, 0, $toCount); return $fillUp(array_merge($filledUpArray, $initalArray), $missingCount - $initialArrayCount); }; return $fillUp($initalArray, $toCount - $initialArrayCount); } $theArray = array("a", "b", "c", "d"); $toLength = 71; $filledArray = fill($theArray, $toLength); print_r($filledArray); 
0
source share
 $array = array("a", "b", "c", "d"); $merge = array(); $desiredLength = 71; while(2 * count($array) <= $desiredLength){ $array = array_merge($array, $array); } if($desiredLength > count($array)) $merge = array_slice($array, 0, $desiredLength - count($array)); $array = array_merge($array, $merge); $array = array_slice($array, 0, $desiredLength); print_r($array); 
0
source share
 $arr = array("a", "b", "c", "d"); $len = 71; $a = array(); $a = str_split( substr( str_repeat( join( $arr), ceil( $len / count( $arr))), 0, $len)); var_export($a); 
0
source share

I think user2469998 was the closest, but just not very pleasant.

In my example, I use pipe for implode and the str_repeat function to build a string that matches the length, blow it up and split the fat.

 $list = array('a','b','c','d'); $length = 6; $result = array_slice(explode('|', str_repeat(implode('|', $list).'|',ceil($length/count($list)))), 0, $length); 

Many ways to achieve this, but I thought I would share with you. The only limitation is that you need to use the symbol for hacking and exploding, which does not include an array element, or exploder will not work properly.

:)

0
source share

All Articles