PLEASE HELP!!!
I have a 2 dim array (5x5) - I want to sort random integers that are contained within the array (from smallest to largest).... When I use asort() function I get the orginal table back... Also, the bubble sort didn't work... it only sorts the rows indpendently....

Reference:/// Can someone PLEASE Help and point out what is wrong.... feel free to write back a few lines of code that may work....

Thanks!

<?php
  // Assign values ....  

    $table = array();
    $x=0;
    $min=60;
    $max=101;

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

                    echo "<tr>\n";

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

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

//Try her to re-display the table using BUBBLE SORT each row gets sorted but not the entire table...  

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

                    echo "<tr>\n";

                      for($col=1;$col<=5; $col++){

                          for($j=1;$j<=5;$j++){

                          if ($table[$row][$col]>$table[$row][$col+1]){

                              $temp=$table[$row][$col];
                              $table[$row][$col]=$table[$row][$col+1];
                              $table[$row][$col+1]=$temp;
                          }
                              }

                                                     }
                       echo "</tr>";
                                  }
    echo "</table>";     

// 
asort($table);  // The asort() does not affect the table - it gives back the same one....

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

                    echo "<tr>\n";




                      for($col=1;$col<=5; $col++){

                                  $y= $table[$row][$col];

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


  :)

Recommended Answers

All 5 Replies

Sorting a multi-dimension array takes some pre-work before you sort to make it work. Follow the link and have a look. I think that will solve your problem.
Click Here

Thanks for your quice response!

Apparently, I do have knowledge on how to do arrays... but the link you gave me wasn't very helpful... do you have any other solutions?

Thanks!

I don't intend to debug your code but I can tell you that the code that I sent you the link for will sort a two-dimensional array. When I originially put it together, I spent a lot of time researching how to do it. This solution was based on what I found. If you don't find it helpful then I obviously don't understand your problem. That's all I have for you so good luck.

Be so kind as to provide some sample input and output, perhaps that will clear up what you want to achieve.

Well, I am trying to take a table that is randomly sorted, and I wanted to put it in order (biggest to smallest)... apparently, whatever I do, it doesn't work... And I filled the array up with a loop... (maybe my loop is designed wrong?)

That's what I am trying to do!
Thanks!

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.