I have five value that I get from generating random function in PHP like this:
1
0
0
1
1
And this is the code:
for ($i=0; $i<10; $i++){
$n = rand(0, 1);
echo $n;echo "<br/ >"}
And now, what should I do so that I only get the value from the first row and the third? So I only get the value:
1
0
Use an array where you store the data and then access to it:
for ($i=0; $i<10; $i++){
$data[] = rand(0, 1);
}
echo $data[0]; //1st "row"
echo '<br>';
echo $data[2]; //3rd "row"
Keep in mind that PHP arrays start with the index 0. To learn more about PHP arrays and its usage: http://php.net/manual/en/language.types.array.php
Store your values in an array then output the ones you actually want
$n = array();
for ($i=0; $i<10; $i++){
$n[] = rand(0, 1);
}
echo $n[0];
echo '<br>';
echo $n[2];
Something like this:
for($i=0; $i<10; $i++)
{
$n = rand(0, 1);
if($i != 0 && $i != 2)
continue;
echo $n;
echo "<br/ >";
}
working example here
for ($i=0; $i<10; $i++){
if($i == 0 || $i == 2){
$n = rand(0, 1);
echo $n;echo "<br/ >"
}
}
but I have no idea what you want to achieve with that...
Let's get rid of the for loop:
$data = array_map(function($v){return rand(0,1);}, range(0, 9)); // Requires PHP 5.3+
echo $data[0] . ' ' . $data[2];