2017-04-18 16 views
1

我是新来的PHP,我正在做这个项目来教自己一点。保存/ Alphabetize在一个Foreach数组中的XML响应

我从路透社的RSS源导入XML数据,并希望按字母顺序排列所有回复的内容。我没有问题使用foreach循环加载我想要的页面信息,但是我使用的排序系统按字母顺序排列每个xml标题中的单词,而不是一起作为一个字符串。

如何将所有响应分组或保存在一起,以便在foreach循环收集后将它们排序为整体?

这是我到目前为止有:

<?php 

function getFeed($feed_url) { 

$content = file_get_contents($feed_url); 
$x = new SimpleXmlElement($content); 
$string = $x->channel->item ; 

echo "<p>"; 

foreach($x->channel->item as $entry) { 
    $string = $entry->title; 
    $split=explode(" ", $string); 
    sort($split); // sorts the elements 
    echo implode(" ", $split); //combine and print the elements 
} 

echo "</p>"; 

}?> 
+0

很辛苦没有XML样本,以帮助这些变量可能意味着什么!另外,我很有兴趣看到这种独特的排序方式:*将每个xml标题中的单词单独分开,而不是一起作为一个字符串*。所以它改变了每个标题中的词语? – Parfait

+0

我正在使用的XML文档在这里:http://feeds.reuters.com/Reuters/PoliticsNews – samseurynck

+0

我的意思是通过字母排列标题在一起是这样的: “政府允许避免变脸政府移民印度共和党人可能共和党人规则关机任务的技术人员那恐怖的对棘手的王牌王牌USUS受害者“
作为反对...... ”特朗普政府可能会改变规则,允许恐怖受害者移民到USIndian技术人员 特朗普,共和党面临避免美国棘手的任务政府关闭“ – samseurynck

回答

0

你所想do是用所有单词构建一个数组,然后在最后对其进行排序。

<?php 

function getFeed($feed_url) {  
    $content = file_get_contents($feed_url); 
    $x = new SimpleXmlElement($content); 
    $titles = ""; 
    foreach($x->channel->item as $entry) { 
     $titles .= " $entry->title"; 
    } 
    $split = explode(" ", $titles); 
    sort($split, SORT_FLAG_CASE | SORT_NATURAL); 
    return trim(implode(" ", $split)); 
} 

$words = getFeed("http://feeds.reuters.com/Reuters/PoliticsNews"); 

echo "<p>$words</p>"; 

我没有删除非单词字符,所以像引号之类的东西会混乱排序。

输出:

<p>'sanctuary' abuse after announcement, areas as battle California, cards, case crises cut detention drill Egyptian Egyptian-American Exxon financial Florida for freed from funding future give green greets healthcare Homeland hope in in in in Indian looms not of officials orders orders other permission plan possible prevent probe probes racial reboot reform resigns revamped review review review rule rules Russia Security see seeks senator sets slur state summons tax tax testify threatens to to to to Top Trump Trump Trump Trump Trump Trump-Russia Twitter U.S. U.S. U.S. U.S. Uphill visa-holders Waiting Washington will</p> 
+0

啊,就是这样!谢谢。 – samseurynck

0

考虑节约标题到一个数组,整理它们的值,然后重复背出echo输出:

$content = file_get_contents("http://feeds.reuters.com/Reuters/PoliticsNews"); 
$x = new SimpleXmlElement($content); 

$titles = []; 
foreach($x->channel->item as $entry) { 
    $titles[] = $entry->title; 
} 

sort($titles, SORT_NATURAL | SORT_FLAG_CASE); # CASE INSENSITIVE SORT 

foreach($titles as $t) { 
    echo "<p>". $t ."</p>"; 
} 

# <p>Ex-Illinois Governor Blagojevich's 14-year prison term upheld</p> 
# <p>Exxon probe is unconstitutional, Republican prosecutors say</p> 
# <p>Group sues Trump for repealing U.S. wildlife rule in rare legal challenge</p> 
# <p>Indian techies, IT firms fret as Trump orders U.S. visa review</p> 
# <p>Trump, Republicans face tricky task of averting U.S. government shutdown</p> 
# <p>Trump administration may change rules that allow terror victims to immigrate to U.S.</p> 
# <p>U.S. House committee sets more hearings in Trump-Russia probe</p> 
# <p>U.S. judicial panel finds Texas hurt Latino vote with redrawn boundaries</p> 
# <p>U.S. retailers bet on Congress over Bolivia to thwart Trump border tax issue</p> 
# <p>U.S. Treasury's Mnuchin: Trump to order reviews of financial rules</p>