8

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

4

10 回答 10

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);
于 2010-08-29T17:11:33.430 回答
3

使用 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')));

真正的 SPL 黑客可能已经放弃了if (!$length--) break;,而是使用了限制迭代器:new LimitIterator(new InfiniteIterator(new ArrayIterator($values)), 0, $length),但我认为这太过分了......

于 2010-08-29T18:50:30.533 回答
2

使用each()andreset()和数组的内部指针的简单解决方案:

<?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
于 2010-08-29T18:18:22.810 回答
2

为了加入这个俱乐部:

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

你要求最快,所以我做了一个基准测试(来源: 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 是最快的,但它仅适用于具有单个字符的字符串值(或者如果使用 str_split 的第二个参数,则长度相同)。

于 2013-08-21T11:51:54.473 回答
0
$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);
于 2010-08-29T17:59:23.800 回答
0
$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;
    }
}
于 2010-08-29T17:02:37.013 回答
0
<?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>';
于 2010-08-29T17:08:44.537 回答
0
$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);
于 2013-06-10T07:27:24.410 回答
0

如果你有 PHP 5.3 可用,你也可以试试这个:

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);
于 2010-08-29T17:29:12.527 回答
0

我认为 user2469998 是最接近的,但不是那么好。

在我的示例中,我使用管道内爆和 str_repeat 函数来构建符合长度的字符串,将其炸开并修剪脂肪。

$list = array('a','b','c','d');

$length = 6;

$result = array_slice(explode('|', str_repeat(implode('|', $list).'|',ceil($length/count($list)))), 0, $length);

实现这一目标的方法很多,但我想我会分享我的。唯一的限制是您需要使用不属于数组项的角色来内爆和爆炸,否则爆炸器将无法正常工作。

:)

于 2017-02-05T15:31:58.040 回答