How do I sort this loop? I can't sort it because I just began learning PHP, so I don't know as much as most of you users... Can anyone help me?

<?php
    $red = array();
    $x=0;
    $min=500;
    $max=2000;

    echo "<table border=\"1\">";
    for($row=1;$row<=20; $row++){

                    echo "<tr>\n";
                    //Need table ONE to have 20 by 20 rows & columns 
                    //Need the table to contain numbers between 900 - 2000 (Randomly)
                    //Table two will take table ONE and sorts it from Smallest to Biggest
                    //Table three will take table ONE and sorts it from Biggest to Smallest
                    //Table 4 will only show a (*) net to the Odd Numbers and (#) next to the Even Numbers 

                      for($col=1;$col<=20; $col++){
                          $x=rand($min,$max);
                                   $red[$row][$col] = $x;
                                   $blue[$row][$col]=$table[$row][$col];

                                     echo"<td>$x</td>\n"; 
                                                     }
                       echo "</tr>";
                                  }
    echo "</table>"; 




$blue= array();
echo "<table border=\"1\">";

for($row=1;$row<=20; $row++){
    echo "<tr>\n";

for($col=1;$col<=20;$col++){
    $blue[$row][$col] = $y;
    echo"<td>$y</td>\n";
    }
echo "</tr>";
}
echo "</table>"; 
?>

Basically what is in the comments inside the code is what I need to achieve... I unfortunately can't, so please help me!!!

Dani AI

Generated

A few quick fixes before a working approach: in @M/'s original code there are undefined variables ($table and $y) and some assignments that overwrite values unintentionally. PHP arrays are 0‑based, so building a flat list of 400 values and then chunking into rows avoids off‑by‑one confusion. 's hint about using a prebuilt set (range + shuffle) is useful if you want 400 unique numbers; if duplicates are acceptable, generate 400 random entries instead.

Concept: create a single flat array of 400 numbers (each 900–2000), split it into 20-element rows with array_chunk() to make a 20x20 grid, then render that grid. To get the sorted tables, copy the flat array, run sort() (ascending) or rsort() (descending) and chunk again. To mark odd/even, append '*' for odd and '#' for even when rendering.

Example (modern, self-contained):

<?php
$values = [];
for ($i = 0; $i < 400; $i++) {
    $values[] = function_exists('random_int') ? random_int(900, 2000) : mt_rand(900, 2000);
}

function render_table(array $grid, $markOddEven = false) {
    echo "<table border=\"1\">\n";
    foreach ($grid as $row) {
        echo "<tr>";
        foreach ($row as $n) {
            $suffix = $markOddEven ? ($n % 2 ? '*' : '#') : '';
            echo "<td>{$n}{$suffix}</td>";
        }
        echo "</tr>\n";
    }
    echo "</table>\n";
}

$grid = array_chunk($values, 20);
render_table($grid);                       // original random table

$asc = $values; sort($asc, SORT_NUMERIC);
render_table(array_chunk($asc, 20));      // ascending

$desc = $values; rsort($desc, SORT_NUMERIC);
render_table(array_chunk($desc, 20));     // descending

render_table($grid, true);                // odd (*) / even (#)
?>

Troubleshooting notes: use SORT_NUMERIC for numeric sorting to avoid string comparisons; sort functions modify arrays in place, so copy first if you need the original order preserved; random_int() requires PHP 7+, fall back to mt_rand() if necessary. If you need unique values only, follow 's idea: build range(900,2000), shuffle() it, then array_slice() the first 400 values.

Recommended Answers

All 5 Replies

Is this a homework assignment? If so, we will not just give you the answer. I will, however, help you find the answer yourself.

You will need the functions range(), shuffle(), and sort(). Look these up on php.net.

No, this isn't for a class of any sort... this is just me trying to figure out different ways of manipulating arrays...

Apparently I want to use a loop to sort these... you know how to set this up?

Thanks for the quick response!

Pretty much you need to make an array filled with the numbers from 500-2000, then shuffle the array and display it using a loop. For the other tables, you run the array through a sort function to sort ascending/descending and display the same way you did above.

Can you write me a couple of lines on how to do it? Apparently, I have messed up numerous times today in making this work... do you mind helping?

Thanks again!

Here is part of the code to get you started. In this case, I hate just giving code, but it is usually easier to show you.

<?php

$numbers = range(900,2000);
shuffle( $numbers );

//loop through $numbers and display in table

$numbers_asc = $numbers_desc = $numbers;

asort( $numbers_asc,SORT_NUMERIC );

//loop through $numbers_asc and display in table

arsort( $numbers_desc,SORT_NUMERIC );

//loop through $numbers_desc and display in table

?>

There might be some errors in there, I wrote it pretty quickly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.