note 13969 deleted from function.sort by derick

From: Date: Wed, 31 Aug 2005 10:36:54 +0000
Subject: note 13969 deleted from function.sort by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-94493@lists.php.net to get a copy of this message
Note Submitter: erik at cobram dot com dot br ---- 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!!!

« previous php.notes (#94493) next »