让我们说我有一个这样的数组:
$numbers = array("one", "two", "three", "four", "five");
如果数组少于5个元素,在php中重复值的最佳方法是什么? 例如,如果数组是:
$numbers = array("one", "two", "three")
我想得到这样的结果:
$numbers = array("one", "two", "three","one", "two").
如果数组包含2个元素,我想要这样的结果:
$numbers = array("one", "two", "one", "two", "one").
如果数组包含1个元素,我想要这样的结果:
$numbers = array("one", "one", "one", "one", "one").
答案 0 :(得分:2)
试试这个:
$numbers = array("one", "two");
$count = count($numbers);
for($i=$count; $i<=4;$i++){
$numbers[] = $numbers[$i % $count];
}
答案 1 :(得分:0)
如果您希望数组具有随机的附加值,可以使用以下内容: -
$numbers = array("one", "two", "three");
while(count($numbers) < 5)
{
$numbers[] = $numbers[mt_rand(0,count($numbers))];
}
var_dump($numbers);