note 13969 added to function.sort.php

From: Date: Thu, 12 Jul 2001 02:21:38 +0000
Subject: note 13969 added to function.sort.php
Groups: php.notes 
Request: Send a blank email to php-notes+get-15266@lists.php.net to get a copy of this message
Just to extend the topic re: sorting arrays with just one item... I have had the same problem! I don't remember anything in the books I read about this, and haven't seen it in the manual, but here's what I did to fix the problem for me: if ($link_keys){ if (count($link_keys) <= 1){ array_push($link_keys, "-"); echo "<br>Link_keys:<br>"; rsort($link_keys); array_pop ($link_keys); while (list($lvar, $lvalue) = each($link_keys)){ echo "$lvar = $lvalue<br>"; //DEBUG: Shows $post_vars_keys index and values. } } else { sort($link_keys); echo "<br>Link_keys:<br>"; //echo "Num of Elements: " . count($link_keys) . "<br>"; while (list($lvar, $lvalue) = each($link_keys)){ echo "$lvar = $lvalue<br>"; //DEBUG: Shows $post_vars_keys index and values. } } } It basically says: if such an array exists and its size is less than or equal to 1 [element], add a "dummy" entry, reverse sort it and pop-off the last item -> which should be the dummy entry! array[1] becomes array[0]! Else sort the array just the way it is. Ofcourse, this assumes you have consistent entries in your arrays to be sorted or at least: that you use some distinct character which YOU KNOW won't appear in the life of that array. Hope this helps at least some people!!! http://www.php.net/manual/en/function.sort.php

« previous php.notes (#15266) next »