More efficient methods?

From: Date: Wed, 21 May 2003 21:11:54 +0000
Subject: More efficient methods?
Groups: php.general 
Request: Send a blank email to php-general+get-148448@lists.php.net to get a copy of this message
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. The RSS feed has a 'link', 'title', 'description' fields. The description field has the format: [category] description text So, to get the category split out I am doing the following to stuff the dsecription into two fields-- can this be combined or shortened?: ereg("(^\[[A-Za-z]*\])(.*)", $item->description, $regs); $regs[1] = str_replace("[", "", $regs[1]); $regs[1] = str_replace("]", "", $regs[1]); $roll[$counter]['type'] = $regs[1]; $roll[$counter]['description'] = $regs[2]; 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 I need to output these on the page as: FOO link google link php BAR link chris.com etc 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... <pre> while ($item = $rss->getNextItem()) { ereg("(^\[[A-Za-z]*\])(.*)", $item->description, $regs); $regs[1] = str_replace("[", "", $regs[1]); $regs[1] = str_replace("]", "", $regs[1]); $roll[$counter]['type'] = $regs[1]; $roll[$counter]['link'] = $item->link; $roll[$counter]['title'] = $item->title; $roll[$counter]['description'] = $regs[2]; $rolltypes[$counter] = $regs[1]; $counter++; } echo "</ul>\n"; $cats = $rolltypes; $cats = array_unique($cats); asort($cats); asort($rolltypes); print_r($roll); print "<hr>"; print_r($rolltypes); print "<hr>"; print_r($cats); print "<hr>"; foreach($cats as $cat) { print "<ul><li><strong>$cat</strong></li></ul>"; print "<ul class='subcat'>"; foreach ($rolltypes as $index => $thiscat) { if($cat == $thiscat) { print "<li>"; print "<a href='" . $roll[$index]['link'] . "'"; print " title='" . $roll[$index]['description']; print "'>"; print $roll[$index]['title']; print "</a>"; print "</li>"; } } print "</ul>"; } <pre>

« previous php.general (#148448) next »