PHP array_rand/Loop/array_push/comportamiento de implosión?
Frecuentes
Visto 224 equipos
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.
cuando hago eco $arvottuLottoRivi
y $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);
2 Respuestas
1
Cuando escribes:
foreach($tenNumbersArray as $randomToSiteLisanuimerot){
while($k <= $lottoRivinPituus){
$k++;
}
if($k >= 7){
array_push($lottoLisaNumerot, $randomToSiteLisanuimerot);
}
}
al while
bucle es equivalente a:
$k = $lottoRivinPituus + 1;
Como $lottoRivinPituus
es 10, $k
is always 11. Therefore, if($k >= 7)
is always true, so all elements of $randomToSiteLisanuumerot
se copian a $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]);
}
Respondido 05 Feb 14, 17:02
1
I really didn't get your code.
Por qué no usar fila ¿función?
$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++;
}
}
Respondido 05 Feb 14, 18:02
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
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
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);
y $lottoLisaNumerot = implode(' ', $randomNumbers2);
- pullapooh
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
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas php arrays loops or haz tu propia pregunta.
What's the point of those
while
bucles? - Barmar@Barmar ooh Sorry I had
array_push
function there before. At the moment they do not have any function. - PullapoohCuando ejecuto tu script,
$arvottuLottoRivi
está vacío y$arvottuLottoRivi
has 10 random elements of the input array. - Barmar@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