views:

70

answers:

7

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?

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
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
$new_array will end with 72 characters.
Alexander
@Alexander: thanks, fixed.
Evert
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
@Kau-Boy: thanks, fixed it.
Evert
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
@Kau-Boy: I hope you know an array starts at 0..
Evert
+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
Sorry had some typos before. I tested it and it works as expected with the exact desired number in the result.
Kau-Boy
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
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
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
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