0

我想把变量变成一个数组,这样我就可以存储多个提要?

<?php 
error_reporting(0);

$feed_lifehacker_full = simplexml_load_file('http://feeds.gawker.com/lifehacker/full');

$xml = $feed_lifehacker_full;
//print_r($xml); 

foreach ($xml->channel->item as $node){

$title = $node->title; 
$link = $node->link; 
$link = explode('/', $link);
$link = $link[8];
$url = $node->url;
$description = $node->description; 
$pubDate = $node->pubDate;
preg_match_all('#(http://img[^\s]+(?=\.(jpe?g|png|gif)))#i', $description[0], $images);
$images = $images[0][1] . '.jpg';
if($images == '.jpg'){

    //uncomment to show youtube articles
    //$images = "http://placehold.it/640x360";
    //echo  "<a href='page2.php?a=$link' title='$title'><img src='$images' /></a><br>";
} else {
    //article image
    $images . '<br>';
    echo  "<a href='page2.php?a=$link' title='$title'><img src='$images' /></a><br>";
}


}

如何更改它以加载到数组,

$feed_lifehacker_full = simplexml_load_file('http://feeds.gawker.com/lifehacker/full');

$xml = $feed_lifehacker_full;

该脚本只是收集 rss 提要的图像并链接到页面,如果您看到如何更有效地完成它,请随意说

4

1 回答 1

0

可以将给出的结果编码为 json 并通过解码它会返回一个数组

$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$array = json_decode($json, TRUE);
于 2013-08-28T16:45:26.580 回答