Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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);
share|improve this question
1  
What's the point of those while loops? – Barmar Feb 5 '14 at 17:33
    
@Barmar ooh Sorry I had array_pushfunction there before. At the moment they do not have any function. – Pullapooh Feb 5 '14 at 17:40
    
When I run your script, $arvottuLottoRivi is empty and $arvottuLottoRivi has 10 random elements of the input array. – Barmar Feb 5 '14 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. – Pullapooh Feb 5 '14 at 17:47
up vote 1 down vote accepted

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]);
}
share|improve this answer

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++;
    }
}
share|improve this answer
    
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. – Pullapooh Feb 5 '14 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. – Hotwer Feb 5 '14 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); – Pullapooh Feb 5 '14 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. – Hotwer Feb 5 '14 at 18:02
    
Sorry I didn't see that. Now it's working. – Pullapooh Feb 5 '14 at 18:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.