Help Loading And Displaying Arrays with Loops

First, a DO-WHILE is almost the same as a WHILE except that in a DO-WHILE, it forces the first iteration to run.

Next, anything you want handled must be inside the loop. Therefore, the $i++ is only executed AFTER the loop finishes.
Therefore in your case, you never increment the variable and therefore, the CONDITION never happens and the loop just
keeps going on and on until your server throws an error. Try changing this:
[php]
$x = 0;
do {
Display $A [$x];
} while (x < 14)
$x++;
[/php]
To this:
[php]
$x = 0;
do {
echo $A [$x] = rand (0,999);
$x++;
} while (x < 14)
[/php]
Or whatever you are trying to do…

Alright makes sense that would explain the endless loading on the phptest site.

[php]<?php
$x = 0;
do {
$A [$x] = rand (0,999);
$x++;
} while (x < 14)

$x = 0;
do {
echo $A [$x];
$x++;
} while (x < 14)
?>[/php]

Now getting unexpected variable on line 8. the ‘$x’ again

[php] do {
$ar [$x] = rand (0,999);
$x++;
} while (x < 14);[/php]

[php]for($i = 0; $i < 15;$i++){
$ar [$x] = rand (0,999);
} [/php]

Those both do the exact same thing do they not?

Alright no errors with this code now but Im only getting a single number in my result instead of the 15 I need
[php]<?php
$x = 0;
do {
$A [$x] = rand (0,999);
$x++;
} while (x == 15);

$x = 0;
do {
echo $A [$x];
$x++;
} while (x == 15);
?>[/php]

I missed it, x is throwing an error, should be $x

Alright sweet I got it with this:
[php]<?php
$x = 0;
do {
$A [$x] = rand (0,999);
$x++;
} while ($x < 14);

$x = 0;
do {
echo $A [$x];
$x++;
} while ($x < 14);
?>[/php]

All I need to do now is order the list in ascending order. Do I need to just add an extra conditional statement in the second loop?

Edit: no i would just need an if statement to order an array that I already have created.

Could look it up, Sorting arrays

I did this is what I got but I just get the same list twice and it doesnt sort. Classmate has the same thing and his does.

[php]<?php
$x = 0;
do {
$A [$x] = rand (0,999);
$x++;
} while ($x < 14);

$x = 0;
do {
echo $A [$x];
echo “
”;
$x++;
} while ($x < 14);

asort($A);
echo “

”;

$x = 0;
do {
echo $A [$x];
echo “
”;
$x++;
} while ($x < 14);

?>[/php]

I figured it out. I needed to use sort and not asort.

Thanks for all the help guys I really appreciate it!

That is because you are listing based on the key, which don’t change. That partly is because of the loop you decided to use. Look at a foreach

Glad you solved it! Congratulations! See you in your next programming puzzle!

Sponsor our Newsletter | Privacy Policy | Terms of Service