0

Why does this not make two arrays one within 7 numbers and one within 2 numbers in it?

It somehow combines the both into one.

When i echo $arvottuLottoRivi and $lottoLisaNumerot in my HTML page the result is:

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

all the seven numbers.

I have now tried three different styles but same thing happens in all cases

    // 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);
4
  • 1
    What's the point of those while loops? Commented Feb 5, 2014 at 17:33
  • @Barmar ooh Sorry I had array_pushfunction there before. At the moment they do not have any function. Commented Feb 5, 2014 at 17:40
  • When I run your script, $arvottuLottoRivi is empty and $arvottuLottoRivi has 10 random elements of the input array. Commented Feb 5, 2014 at 17:44
  • @Barmar Ye I have the same result. I will fix the my writing. But it should be $arvottuLottoRivi = 7 numbers and $arvottuLottoRivi = 2 numbers. Commented Feb 5, 2014 at 17:47

2 Answers 2

1

When you write:

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

the while loop is equivalent to:

    $k = $lottoRivinPituus + 1;

Since $lottoRivinPituus is 10, $k is always 11. Therefore, if($k >= 7) is always true, so all elements of $randomToSiteLisanuumerot are copied to $lottoLisaNumerot. Similarly, in the previous loop, the test if ($l <= 7) is always false, so nothing is copied to $arvottuLottoRivi.

I think you were trying to test the current position in the loop, not the count of all elements in the array. You can do it like this:

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

But this wastes time iterating over elements it doesn't care about. A better way would be:

$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]);
}
1

I really didn't get your code.

Why don't use rand function?

$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++;
    }
}

And if the seconds array cannot contains any number within the first one:

$i = 0;
while ($i < 2) {
    $aNumber = rand(1, 39);
    if (!in_array($aNumber, $randomNumbers2) && !in_array($aNumber, $randomNumbers1)) {
        $randomNumbers2[] = $aNumber;
        $i++;
    }
}
4
  • I tried this technique, but I had some problems, it gives me the number 0, and I do not understand why. then I began to try a new way to create that. Commented Feb 5, 2014 at 17:51
  • Do you know how rand function works? int rand ( int $min , int $max ), but if you don't give any arguments, it will give you a number between 32768 and 0. EDIT: Also, I've found out you can try mt_rand() that is better function, if you need more stability, but I'm not the better person to tell you the significant differences between them. Commented Feb 5, 2014 at 17:54
  • Ye I gave it rand(1, 39). I tried your code but almost the same happens. It just echoes the same numbers to both. I added $arvottuLottoRivi = implode(' ', $randomNumbers2);and $lottoLisaNumerot = implode(' ', $randomNumbers2); Commented Feb 5, 2014 at 17:59
  • You're giving both variables the same array. You should give $randomNumber1 to the $arvottuLottoRivi and $randomNumber2 to $lottoLisaNumerot or whatever one should recieve the seven array and the two array. Commented Feb 5, 2014 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.