PHP array_rand / Loop / array_push / implode行为?

时间:2014-02-05 17:29:00

标签: php arrays loops

为什么这不会使两个数组在一个7个数字中,一个在2个数字中呢?

它以某种方式将两者结合为一体。

当我在HTML页面中回显$arvottuLottoRivi$lottoLisaNumerot时,结果为:

$arvottuLottoRivi (1,2,3,4,5,6,7,8,9,10) : $lottoLisaNumerot

所有七个数字。

我现在尝试了三种不同的风格,但在所有情况下都会发生同样的事情

    // VARAIBLES
    $lottoNumerot = $_POST["lottoNumerot"];
    $mahdollisetNumerot = array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39");
    $i = 0;
    $l = 0;
    $k = 0;


    //ARRAYS
    $arvottuLottoRivi = array();
    $lottoLisaNumerot = array();
    $tenNumbersArray = array();


    //FUNCTIONS
    $numeroidenRandomointi = array_rand($mahdollisetNumerot, 10);


    // COUNTS ARRAY LENGHT
    $lottoRivinPituus = count($numeroidenRandomointi);;

    // LOOPS
    foreach($numeroidenRandomointi as $randomNumero){
        while($i <= $lottoRivinPituus){
        $i++;
        }
        $randomToArray = array_push($tenNumbersArray, $randomNumero);
    }


    // LOOPIT
    foreach($tenNumbersArray as $randomToSite){
        while($l <= $lottoRivinPituus){
        $l++;
        }
        if($l <= 7){
            array_push($arvottuLottoRivi, $randomToSite);
        }
    }

    foreach($tenNumbersArray as $randomToSiteLisanuimerot){
        while($k <= $lottoRivinPituus){
        $k++;
        }
        if($k >= 7){
            array_push($lottoLisaNumerot, $randomToSiteLisanuimerot);
        }
    }

    $arvottuLottoRivi = implode(' ', $arvottuLottoRivi);
    $lottoLisaNumerot = implode(' ', $lottoLisaNumerot);

2 个答案:

答案 0 :(得分:1)

我真的没有得到你的代码。

为什么不使用 rand 功能?

$randomNumbers1 = array();
$randomNumbers2 = array();


$i = 0;
while ($i < 7) {
    $aNumber = rand(1, 39);
    if (!in_array($aNumber, $randomNumbers1)) {
        $randomNumbers1[] = $aNumber;
        $i++;
    }
}

$i = 0;
while ($i < 2) {
    $aNumber = rand(1, 39);
    if (!in_array($aNumber, $randomNumbers2)) {
        $randomNumbers2[] = $aNumber;
        $i++;
    }
}

如果秒数组不能包含第一个中的任何数字:

$i = 0;
while ($i < 2) {
    $aNumber = rand(1, 39);
    if (!in_array($aNumber, $randomNumbers2) && !in_array($aNumber, $randomNumbers1)) {
        $randomNumbers2[] = $aNumber;
        $i++;
    }
}

答案 1 :(得分:1)

当你写:

foreach($tenNumbersArray as $randomToSiteLisanuimerot){
    while($k <= $lottoRivinPituus){
    $k++;
    }
    if($k >= 7){
        array_push($lottoLisaNumerot, $randomToSiteLisanuimerot);
    }
}

while循环等同于:

    $k = $lottoRivinPituus + 1;

由于$lottoRivinPituus为10,$k始终为11.因此,if($k >= 7)始终为true,因此$randomToSiteLisanuumerot的所有元素都会复制到$lottoLisaNumerot。同样,在上一个循环中,测试if ($l <= 7)始终为false,因此不会将任何内容复制到$arvottuLottoRivi

我认为你试图测试循环中的当前位置,而不是数组中所有元素的计数。你可以这样做:

foreach($tenNumbersArray as $l => $randomToSite){
    if($l < 7){
        array_push($arvottuLottoRivi, $randomToSite);
    }
}
foreach($tenNumbersArray as $k => $randomToSiteLisanuimerot){
    if($k >= 7){
        array_push($lottoLisaNumerot, $randomToSiteLisanuimerot);
    }
}

但这浪费时间迭代它不关心的元素。更好的方法是:

$arvotSize = min(7, $lottoRivinPituus);
for ($l = 0; $l < $arvotSize; $l++) {
    array_push($arvottuLottoRivi, $tenNumbersArray[$l]);
}
for ($k = $arvotSize; $k < $lottoRivinPituus; $k++) {
    array_push($lottoLisaNumerot, $tenNumbersArray[$k]);
}