Multi-Densional Array Sorting?
| From: | houser at houserdesign dot com | Date: | Mon, 11 Mar 2002 15:41:54 +0000 |
| Subject: | Multi-Densional Array Sorting? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-88017@lists.php.net to get a copy of this message | ||
Okay - here's one for you. Probably something really small and stoopid which I'm
fergeting, but I don't mind a little egg on my face as long as I can figure out the problem.
Since you may or may not be down, I've included plenty o'comments to assist you in looking
at this issue for me - thanks, by the way!
I'm having a problem in the code below. In simple, if you were to copy and paste the code to a
file and run it on a PHP enabled server, you'll see a nifty table. Click on any one of the
column headings and BAM!! - the column gets resorted (either ascending or descending) WITH ONE
EXCEPTION - if you click on the last column heading, be ready to have your task manager window open
so you can kill your browser, or else this puppy will lock your box tighter than a Bavarian virgin!!
It doesn't seem to matter WHAT the last column is, as evident by the last column name in the
below example - previously it would bag on position. Now position works fine, but be wary of test.
So I know it has to be something with the last column, but am having a little difficulty fighting my
way out of this paper bag.
Any thoughts?
Here's the code:
// BEGIN TEST.PHP
// First we'll create a multi-demensional array of dummy info
$arr["id"][] = "1";
$arr["name"][] = "John";
$arr["position"][] = "Developer";
$arr["test"][] = "schtuff";
$arr["id"][] = "2";
$arr["name"][] = "Andrew";
$arr["position"][] = "Senior Product Designer";
$arr["test"][] = "schtuff4";
$arr["id"][] = "3";
$arr["name"][] = "Christina";
$arr["position"][] = "Product Manager";
$arr["test"][] = "schtuff3";
// Next, for this example, we're gonna need a default sort order
// if we haven't defined one, we'll set it to the ID field
if(!isset($order_by)) $order_by = "id";
if($order_dir == "DSC") arsort($arr[$order_by]); // This does a reverse sort on our 2nd
dimension
else asort($arr[$order_by]); // This does a normal sort on our 2nd dimension
// Start spitting out the UI
echo "<table border=1><tr bgcolor=#eeeeee>";
// Walk through each 1st dimension to write the table headers based on the 1st dimension values
foreach($arr as $key => $val){
echo "<td><a href=".$PHP_SELF."?order_by=$key";
if(($key == $order_by)&&(!isset($order_dir))) echo "&order_dir=DSC";
echo ">$key";
if($key == $order_by){ // Getting fancy now, aren't we?
if($order_dir == "DSC") echo "[-]";
else echo "[+]";
}
echo "</a></td>";
}
echo "</tr>";
// Now we'll spit out the actual data
// Go through the now sorted 2nd dimension and get the keys (which holds the data positions
// in the other 2nd dimensions
while(list($key) = each($arr[$order_by])){
echo "<tr>";
// now that we have the 2nd dimension key, we'll walk through the 1st dimension arrays
// one by one and get the value by way of our determined key
foreach($arr as $skey => $sval){
echo "<td>".$arr[$skey][$key]."</td>";
}
echo "</tr>";
}
echo "</table>";
// END TEST.PHP