I'm having 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 certain length, e.g 71 elements?
views:
70answers:
7
A:
$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;
}
}
Hammerite
2010-08-29 17:02:37
A:
<?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 works.
You can test for yourself by adding this:
echo '<pre>';
print_r($new_array);
echo '</pre>';
Evert
2010-08-29 17:08:44
$new_array will end with 72 characters.
Alexander
2010-08-29 17:14:10
@Alexander: thanks, fixed.
Evert
2010-08-29 17:19:59
That will only create array that can be devided by four (e.g. it would produce an array with the size of 76 if you set the end to 73!
Kau-Boy
2010-08-29 17:26:09
@Kau-Boy: thanks, fixed it.
Evert
2010-08-29 17:29:19
That should work, but only if you set "$end + 1" as the length is not starting with 0. If you now replace also your foreach loop with array_merge() that you'll have the exact same code as on my answer ;)
Kau-Boy
2010-08-29 17:33:32
@Kau-Boy: I hope you know an array starts at 0..
Evert
2010-08-29 17:51:55
+2
A:
// 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);
Kau-Boy
2010-08-29 17:11:33
Sorry had some typos before. I tested it and it works as expected with the exact desired number in the result.
Kau-Boy
2010-08-29 17:21:58
A:
If you have PHP 5.3 available, you can also try this:
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);
Max
2010-08-29 17:29:12
A:
$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);
Alexander
2010-08-29 17:59:23
A:
A simple solution using each()
and reset()
and the array's internal 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
Frxstrem
2010-08-29 18:18:22
A:
Solution using SPL InfiniteIterator:
<?php
function fillArray1($length, $values) {
foreach (new InfiniteIterator(new ArrayIterator($values)) as $element) {
if (!$length--) return $result;
$result[] = $element;
}
}
var_dump(fillArray(71, array('a', 'b', 'c', 'd')));
The real SPL hackers might have dropped the if (!$length--) break;
and instead used a limit iterator: new LimitIterator(new InfiniteIterator(new ArrayIterator($values)), 0, $length
, but I thought that to be overkill...
nikic
2010-08-29 18:50:30