I recently started taking classes on PHP and I came across a weird bug on my code which I just don't understand...
I'm probably just not seeing the solution right now and so I decided to post it here so anybody experienced can help me out instead of waiting for monday to ask a teacher.
What I basically want is to create an array that will take 10 numbers ranging from 1-49 and then create another array which will take these values, shuffle them and randomly take them out one by one to display the full array.
I did this for 2 possible arrays but the last array, the last number is not being displayed with my 1-49 range and is crossing that range.
My code is as follows:
<?php
session_start();
$_SESSION['views'];
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$array = range(1, 49);
$numbers = array();
for ($i = 0; $i < 10; $i++) {
shuffle($array);
$numbers[] = $array[0];
unset($array[0]);
}
print ('Primeiro numero <br/>');
print_r($numbers[0]);
print(', ');
print_r($numbers[1]);
print(', ');
print_r($numbers[2]);
print(', ');
print_r($numbers[3]);
print(', ');
print_r($numbers[4]);
print(', ');
print_r($numbers[5]);
print(', ');
print_r($numbers[6]);
print(', ');
print_r($numbers[7]);
print(', ');
print_r($numbers[8]);
print(', ');
print_r($numbers[9]);
for ($i = 0; $i < 10; $i++) {
shuffle($array);
$numbers2[] = $array[0];
unset($array[0]);
}
print ('<br><br>');
print ('Segundo numero <br/>');
print_r($numbers2[0]);
print(', ');
print_r($numbers2[1]);
print(', ');
print_r($numbers2[2]);
print(', ');
print_r($numbers2[3]);
print(', ');
print_r($numbers2[4]);
print(', ');
print_r($numbers2[5]);
print(', ');
print_r($numbers2[6]);
print(', ');
print_r($numbers2[7]);
print(', ');
print_r($numbers2[8]);
print(', ');
print_r($numbers2[9]);
if ($_GET["destroy"] == "destroy")
session_destroy();
else {
if ($_SESSION['views'] > 0) {
$_SESSION['views'] ++;
} else {
$_SESSION['views'] = 1;
}
echo $_SESSION['views'];
}
?>
</body>
</html>
If someone helps me finding the problem with the last number from the 2nd array being displayed wrong that would be a huge help. Thanks in advance.