2015-05-21 25 views
1

我有一個帶有字段標題列表的xml文件。我想將它們存儲在一個數組中,並按字母順序排序,然後通過其中「標題」出現最多。我的實際XML文件在這裏很大(PS:我從一堆XML文件導入)。在PHP中排序和獲取數組中的值的頻率?

簡而言之,這是XML文件外觀的一個例子。

<add overwrite="true"> 
<docs> 

    <field name="id">9637a08df6aa0765</field> 
    <field name="url">http://somewebsite.ca </field> 
    <field name="blogurl">http://www.someblog.com</field> 
    <field name="published">2015-05-21</field> 
    <field name="language">English</field> 
    <field name="title">Stephen Harper</field> 
    <field name="title">Mike Duffy Trial</field> 
    <field name="title">POTUS on Twitter</field> 
    <field name="title">Tim Hortons Closed</field> 
    <field name="title">Stephen Harper</field> 
    <field name="title">The New iPhone</field> 
    <field name="title">Stephen Harper</field> 
</docs> 
</add> 

所以我們假設我從XML文件中獲得title屬性並將它們存儲在一個數組中。然後,我想按字母順序排序該數組,然後按「標題」的頻率排序。

這是我到目前爲止。

<?php 

    $titles_array = array(); 
    $counter = 0; 
    $xml = simplexml_load_file("fields.xml") or die ("Error: Cannot Create Object"); 

    foreach($xml->docs->field as $fields){ 
     array_push ($titles_array, $fields); 
     echo $fields . "<br>"; 
     $counter++; 
    } 

    echo '<p>' . "Sorted Array" . '</p>'; 

    sort($titles_array); 

    for ($a=0; $a<$counter; $a++){ 
     echo $titles_array[$a] . "<br>"; 
    } 

?> 

輸出實際上不是按字母順序? 另外,如何顯示最頻繁的「標題」?

回答

2

你的$fields實際上是整個你的foreach循環中的SimpleXMLElement。你的陣列將整理如果你用這個代替預期:

array_push($titles_array, (string)$fields); 

要算髮生,創建另一個數組:

$titles_count = array(); 

然後在你的循環,做這樣的事情:

if (isset($titles_count[(string)$fields])) { 
    $titles_count[(string)$fields] = $titles_count[(string)$fields] + 1; 
} else { 
    $titles_count[(string)$fields] = 1; 
} 

最後,讓您的鑰匙像這樣的最高計數:

echo array_search(max($titles_count), $titles_count); 

把所有的代碼放在一起這樣的:

<?php 

    $titles_array = array(); 
    $titles_count = array(); 
    $xml = simplexml_load_file("fields.xml") or die ("Error: Cannot Create Object"); 

    foreach($xml->docs->field as $fields){ 
     array_push ($titles_array, (string)$fields); 
     echo $fields . "<br>"; 

     // First we check if the key exists. 
     // Example $titles_count['Tim Hortons Closed'] 
     if (isset($titles_count[(string)$fields])) { 
     // If it does, augment the count by 1; 
     $titles_count[(string)$fields] = $titles_count[(string)$fields] + 1; 
     } else { 
     // If it doesn't yet, set the count to 1; 
     $titles_count[(string)$fields] = 1; 
     } 
    } 

    echo "<p>Sorted Array</p>"; 

    sort($titles_array); 

    for ($a=0; $a<$counter; $a++){ 
     // Let's put the count in for each one: 
     echo $titles_array[$a] . "(" . $titles_count[$titles_array[$a]] . ")" . "<br>"; 
    } 

    echo "<p>Highest key count:</p>"; 

    // Here we get the value with the highest count use max(...) 
    // Then we get it's key (example 'Tim Hortons Closed') 
    echo array_search(max($titles_count), $titles_count); 

?> 
+0

首先感謝爲排序的一部分。它運行良好。你能不能讓第二部分發生什麼事?這讓我困惑lol – mudoskudo

+0

我把一些意見放到了整個代碼中。讓我知道這是否有幫助。 – Jonathan

+0

不錯!再次感謝喬納森。 – mudoskudo