tags:

views:

18

answers:

1

Im using this PHP to get a list of Title's from an RSS feed:

<?php require_once('magpie/rss_fetch.inc');
$rss = fetch_rss('http://live.visitmix.com/Sessions/RSS');

foreach ($rss->items as $item) { 
    $cat = $item['category'];
    $title = $item['title'];
        echo '<li class="'.$cat.'">'.$title.'</li>';
}

?>

I want to use <category> and add it as the class, however the <category> element appears for each <item> 1,2,3,4 or more times depending on the Title. How can I take the category element and seperate each category with a space if there is more than 1?

A: 

what about accumulating the categories in an array and then implode the array?

$categoriesArray = array();

foreach ($rss->items as $item) { 
    array_push($categoriesArray, $item['category']);
}
$categories = implode(" ", $categoriesArray);

EDIT TO ADD

If the categories are cumulated, you can try something like this:

$categoriesByTitle = array();

foreach ($rss->items as $item) { 
        $currentTitle = $item['title'];
        $categories = $categoriesByTitle[$currentTitle];
        if ($categories == NULL) {
            $categories = array();
            $categoriesByTitle[$currentTitle] = $categories;
        }
        if (!in_array($item['category'], $categories)) {
            array_push($categories, $item['category']);
        }
}
foreach ($categoriesByTitle as $title=>$category) {
    // use implode for each title
    $categoryString = implode(" ", $category);
    echo '<li class="'.$categoryString.'">'.$title.'</li>';
}

Check this reference for arrays, it could be very useful for dealing with this kind of problems

Jhonny D. Cano -Leftware-
But how can i apply the categories to the correct list item. This gives me an array of all categories.
danit
can you post a sample of the XML you are dealing with?
Jhonny D. Cano -Leftware-