Re: More efficient methods?
| From: | Ernest E Vogelsinger | Date: | Wed, 21 May 2003 22:02:03 +0000 |
| Subject: | Re: More efficient methods? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-148458@lists.php.net to get a copy of this message | ||
At 23:11 21.05.2003, Chris said:
--------------------[snip]--------------------
>I am writing a bit of code that takes a blogroll RSS file and parses it into
>an array for some sorting and categorized output. Being the pseudo-
>programmer that I am, I am wondering if I am missing some ways to make my
>code more efficient.
>
>....
>
>I also have a more complex sorting question. Once I get the array populated
>it looks like this:
>$roll[0][type] => foo
>$roll[0][link] => http://google.com
>$roll[1][type] => bar
>$roll[1][link] => http://chris.com
>$roll[2][type] => foo
>$roll[2][link] => http://php.net
>
>...
>
>Because the array is multidimensional, I currently:
>
>1) create a second array of types (X) and their associated index
>2) copy that array to another array (Y) and use array_unique to get one of
>each category
>3) loop over (Y) and for each cat loop over (X) and output the proper links
>
>This gives me the code below, but it seems really roundabout, and of course
>sorting the links themselves alphabetically looks about impossible...
>
--------------------[snip]--------------------
Why don't you use usort, using your own comparison function?
http://www.php.net/manual/en/function.usort.php
Example:
function compare_roll($rolla, $rollb)
{
if ($rslt = strcasecmp($rolla['type'], $rollb['type']))
return $rslt;
return strcasecmp($rolla['link'], $rollb['link']);
}
// $roll is unsorted here
usort($roll, 'compare_roll');
// $roll is sorted after this line
--
>O Ernest E. Vogelsinger
(\) ICQ #13394035
^ http://www.vogelsinger.at/