Re: Sorting an array

From: Date: Thu, 13 Sep 2001 09:10:03 +0000
Subject: Re: Sorting an array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-67161@lists.php.net to get a copy of this message
here's your script, <? $mem = "The Childhood A Man called lallous The Long kiss good night Abbey Road Dark Side Of the Moon The Final Cut The Zombie A Hard Days Night Kill 'em All The Wall"; $mem = split("\n", $mem); $arr1 = array(); for ($i=0;$i<count($mem);$i++) { $line = trim($mem[$i]); $prefix = ""; $affix = $line; if (preg_match("/^(the |a )(.+)$/i", $line, $matches)) { $prefix = $matches[1]; $affix = $matches[2]; } $arr1[$affix] = $prefix; } ksort($arr1); $arr2 = array(); while (list($key, $val) = each($arr1)) { $arr2[] = "$val$key"; } var_dump($arr2); ?> "Dotan Cohen" <dusty351@yahoo.com> wrote in message news:20010913065918.56717.qmail@web14602.mail.yahoo.com... > I'm trying to sort a listing of albums alphabetically > for LyricsList.com, the problem is that I want the > initial 'The' and 'A' in some albums (A Hard Days > Night, The Wall) to be ignored. So that the list will > look like: > > Abbey Road > Dark Side Of the Moon > The Final Cut > A Hard Days Night > Kill 'em All > The Wall > > ABC order, 'A' and 'The' negated. The info comes from > a database, in three calls: > > // call albums that begin with letter $letter > $query = "SELECT * FROM albums WHERE album LIKE > '$letter%' ORDER BY album"; > $result = mysql_query($query) or die(mysql_error()); > > // call albums that begin with letter A > $query_a = "SELECT * FROM albums WHERE album LIKE 'a' > ORDER BY album"; > $result_a = mysql_query($query_a) or > die(mysql_error()); > > // call albums that begin with letter T > $query_t = "SELECT * FROM albums WHERE album LIKE 't' > ORDER BY album"; > $result_t = mysql_query($query_t) or > die(mysql_error()); > > So I need to get just the albums from $result_t that > begin with 'The ' (easy regex) and from $result_a that > begin with 'A ' (easy regex), but puting the three of > them in order is proving beyond my skill. > > Thanks in advance for any advise. > > Dotan Cohen > > > __________________________________________________ > Terrorist Attacks on U.S. - How can you help? > Donate cash, emergency relief information > http://dailynews.yahoo.com/fc/US/Emergency_Information/

« previous php.general (#67161) next »