2017-04-18 2 views
1

저는 PHP에 익숙하지 않고이 프로젝트를 조금만 가르치고 있습니다.Foreach Array에서 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

제목을 함께 알파벳순으로 표기하면 의미는 다음과 같습니다. "행정부가 얼굴을 바꾸는 것을 허용합니다. 규칙 종료 작업에 트럼프 관리 테러 피해자 USIndian의 기술 전문가 트럼프에 이민을 허용하는 규칙을 변경할 수 있습니다 "... 가 반대로
"까다로운 트럼프 트럼프 USUS 피해자에, 공화당이 미국을 피하는의 까다로운 작업을 직면 테러를 기술 전문가 정부 폐쇄 " – samseurynck

답변

0

당신이 원하는 무엇 여기

내가 지금까지 가지고 무엇을 모든 단어를 가진 배열을 만든 다음 끝에 정렬하십시오.

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