English 中文(简体)
PHP + RSS 重复元素
原标题:PHP + RSS duplicate elements
  • 时间:2010-03-03 21:19:01
  •  标签:
  • php

我正在使用这个PHP从RSS提取标题列表:

<?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> ;
}

?>

我想使用<category>将其添加为类,但是每个<item>都会出现<category>元素,具体取决于标题出现1,2,3,4次或更多。如果有多个类别,我该如何将类别元素分开并用空格分隔?

最佳回答

把分类积累到一个数组中,然后将该数组合并起来怎么样?

$categoriesArray = array();

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

编辑添加

如果累积了类别,你可以尝试像这样做:

$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> ;
}

请查看数组的参考资料,它在处理这类问题时非常有用。

问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签