Someone emailed me today to ask me how they could sort results, returned from my PhpDelicious class, alphabetically by description. It seems to me that the solution would probably be useful to other people hence the post.
<?phprequire('php-delicious.inc.php');function SortByDesc($sA, $sB) {return strcmp($sA['desc'], $sB['desc']);}$oDelicious = new PhpDelicious(YOUR_USERNAME, YOUR_PASSWORD);if ($aPosts = $oDelicious->GetAllPosts()) {usort($aPosts, 'SortByDesc');echo "<ul>";foreach ($aPosts as $aPost) {echo "<li><a href=\"{$aPost['url']}\">{$aPost['desc']}</a></li>";}echo "</ul>";}?>
It uses the usort function which sorts array values using a user-defined comparison function - in this case SortByDesc. This custom function uses strcmp, which does a case sensitive string comparison, to determine which parameter should appear first. If you want to do a case insensitive comparison instead replace strcmp function with strcasecmp.
strncmp and strncasecmp which compare the first n characters of each word, case-sensitively and case-insensitively respectively, are also worth a look.
exactly what I am looking for! thanks for sharing about this Ed! I'm too noob in programming and I can't figure this out myself without your tutorial.
*bows down*
Hi - can you help? Bit of a noob, but I downloaded and copied phpdelicious to use on a project - using the code above. But viewing the page I get this error:
Notice: Use of undefined constant CACHE_PATH - assumed 'CACHE_PATH' in C:\wamp\www\testing\cache.inc.php on line 44
Any ideas? Thanks...