Reputation: 1046
I want to generate random numbers between 0 and 1, and push them to two dimension array. And i'm getting this error:
Cannot use a scalar value as an array
This is my code:
<?php
$zero = $one = $rand = 0;
$arr = array(array());
for($i = 0; $i < 5; $i++) {
$arr[$i] = $rand;
for($j = 0; $j < 10; $j++) {
$rand = mt_rand(0,1);
if ($rand == 0) {
$one++;
} else {
$zero++;
}
$arr[$i][$j] = $rand;
echo $arr[$i][$j];
}
echo "<br/>";
}
?>
Upvotes: 1
Views: 153
Reputation: 219814
$arr[$i]
is a scalar value as you are assigning it an integer here:
$arr[$i] = $rand;
There for it is not an array. But you are trying to access it like an array which throws this error:
$arr[$i][$j] = $rand;
You either need to make that value an array or use a different variable for holding your array data. But you can't do both at once.
Upvotes: 1