Re: Filling an array with random input doesn't quite work [message #185215 is a reply to message #185211] |
Wed, 12 March 2014 20:34 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma:
|
Senior Member |
|
|
On Wed, 12 Mar 2014 13:18:19 -0400, richard wrote:
> $x=1;
> while ($x<40){
> $yr=range(60,69); shuffle($yr); $ayr[$x]=$yr[$x]; echo
> $ayr[$x]."<br>";
> $x++;
> }
>
>
> I am attempting to load an array based upon the range of numbers 60
> through 69.
> The array will have 40 items.
> This code produces 9 items just fine.
> The rest of the array is empty.
> What am I missing?
You're missing a richardian fuck up in the array indexing!
You also seem to be ignoring a message something like:
"PHP Notice: Undefined offset: 38 in /richardian_array_fuckup.php on
line 13"
Try:
$x=1;
while ($x<40){
$yr=range(60,69);
shuffle($yr);
$n = count($yr);
echo "\$x is {$x}, size of \$yr is {$n} elements, \$yr[\$x] is '{$yr
[$x]}'<br>\n";
$ayr[$x]=$yr[$x];
echo $ayr[$x]."<br>\n";
$x++;
}
and see if that helps you work it out.
By the way, the following code might be useful:
$a=40;
$b=array();
$c=range(60,69);
while(--$a)$b[$a]=$c[array_rand($c)];
assuming that what you actually want is an array of 39 elements indexed
1..39 with random values from 60 to 69.
I doubt that's exactly what you do want, but that's my best guess at what
your code is intended to create.
--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
|
|
|