NYCPHP Meetup

[nycphp-talk] Sorting 2-dimensional arrays at 2:00 am - UGH!

Phil Powell soazine at erols.com
Mon Aug 11 12:46:24 EDT 2003


Oh yeah I got it BTW, at 4:00am

And rethinking my data structures is never an option; I'll just make it even
more complicated by doing so!  In fact, PHP is the saving grace behind the
idea of sorting data that is parsed from an XML file into a TCL
1-dimensional list.  Otherwise, it would never be done at all.

I have this data that consists of feedback information, when users enter
feedback from my site.  The feedback XML row will consist of:

firstname
lastname
email
url
categoryID (feedback category ID maps to feedbackCategories.xml which has
like 1 => "trivia" or 2 => "articles"...)
timeStamp
showEntry (a boolean switch to "delete" this entry upon choice)

I use TCL to parse the XML file (long story, trust me) and it converts it to
a 1-dimensional list.  However, I would need to sort this one-dimensional
list as follows:

1) sort first by ascending category_id
2) sort each separated category_id by descending timestamp order

So, long in short, this is what I came up with:

// FUNCTION TO BE USED IN uksort() TO RETURN A REVERSE-CASE-INSENSITIVE
BOOLEAN FOR REVERSE SORTING
  function strrevcasecmp($a, $b) {
   if (strcasecmp($a, $b) == 0) return 0;
   return (strcasecmp($a, $b) > 0) ? -1 : 1;
  }

...// reparse the original feedback.xml file instead of using the TCL list
(the TCL script will have updated the existing XML file if need be prior to
calling this PHP script)

// SORT THE OUTER ARRAY FIRST IN CATEGORY_ID ASC AND THEN EACH INNER ARRAY
BY TIME DESC
   // BE SURE TO SUPPRESS WARNINGS IF NO DATA IS FOUND IN fbArray
   @ksort($fbArray);
   if (sizeof($fbArray) > 0) {
    foreach ($fbArray as $key => $val) {
     @uksort($val, "strrevcasecmp");
     $fbArray[$key] = $val;
    }
   }

And there you have it, an overcomplicated solution, once again, possibly an
extremely simpler solution out there that I simply can't fathom. :(

Phil
----- Original Message -----
From: "Hans Zaunere" <hans at nyphp.org>
To: "NYPHP Talk" <talk at lists.nyphp.org>
Sent: Monday, August 11, 2003 12:42 PM
Subject: Re: [nycphp-talk] Sorting 2-dimensional arrays at 2:00 am - UGH!


>
>
> Phil Powell wrote:
>
> > $fbArray[$j] = array($feedbackCategoryArray[$i]['attributes']['NAME'] =>
> > $feedbackCategoryArray[$i]['attributes']['DISPLAYNAME']);
> >
> > I can't begin to fathom in my befuddled mind how to sort this array
> > $fbArray into alphabetical order according to
> > $feedbackCategoryArray[$i]['attributes']['NAME']
>
> I'll throw this function out there as an example:
>
>    function __sortOn( $metadata,$sort_flags,$reverse = FALSE ) {
>
>       if( !isset($this->Listing[0][$metadata]) )
>          return FALSE;
>
>       $metakeys = array();
>       foreach( $this->Listing as $key => $sorton )
>          $metakeys[$key] = $sorton[$metadata];
>
>       if( $reverse )
>          arsort($metakeys,$sort_flags);
>       else
>          asort($metakeys,$sort_flags);
>
>       $tmp = array();
>       foreach( $metakeys as $key => $metakey )
>          $tmp[] = $this->Listing[$key];
>
>       $this->Listing = $tmp;
>
>       return TRUE;
>    }
>
> > - I am utterly lost,
> > can someone explain in detail how I would do it and why.
>
> I have no idea why, but I'd consider rethinking your data structures a bit
:)
>
> H
>
>
> _______________________________________________
> talk mailing list
> talk at lists.nyphp.org
> http://lists.nyphp.org/mailman/listinfo/talk




More information about the talk mailing list