2017-04-17 66 views
0

無法找到正確的方法來執行以下操作:我有xml,其中包含元素,其中每個元素具有屬性順序,值範圍爲0-3。它們按該屬性排序(0,1,2,3,0,1,2,3 ...)並且是兄弟姐妹。一些元素可能會有所不同,並且文檔中的第一個元素不以0開頭,因爲XML每4小時刷新一次,並且每刷新一次,當前的第一個元素get被刪除,並且行中的下一個元素佔據第一個位置。例如:在凌晨0:00,第一個元素的屬性order =「0」。在6:00它被移除,下一個兄弟姐妹需要他(第一個)屬性order =「1」的地方。PHP - 從元素組中檢索最低和最高值

我想爲0-3的一組元素(第一組元素可能有0,1,2或3的順序)進行循環,並檢索最低和最高每個組子節點的值。例如:

Lopping the bottom structure should print: 

11, 82 
2, 92 
1, 211 
... 

<parent> 

    <!-- Group 1 --> 
    <element order="2"> 
     <node value="30" /> 
     <node value="82" /> <!-- This is the highest of the Group 1 --> 
     <node value="25" /> 
    </element> 
    <element order="3"> 
     <node value="12" /> 
     <node value="52" /> 
     <node value="11" /> <!-- This is the lowest of the Group 1 --> 
    </element> 

    <!-- Group 2 --> 
    <element order="0"> 
     <node value="21" /> 
     <node value="78" /> 
     <node value="33" /> 
    </element> 
    <element order="1"> 
     <node value="35" /> 
     <node value="57" /> 
     <node value="88" /> 
    </element> 
    <element order="2"> 
     <node value="22" /> 
     <node value="92" /> <!-- This is the highest of the Group 2 --> 
     <node value="81" /> 
     <node value="19" /> 
    </element> 
    <element order="3"> 
     <node value="2" /> <!-- This is the lowest of the Group 2 --> 
     <node value="30" /> 
     <node value="44" /> 
    </element> 

    <!-- Group 3 --> 
    <element order="0"> 
     <node value="12" /> 
     <node value="99" /> 
     <node value="43" /> 
    </element> 
    <element order="1"> 
     <node value="65" /> 
     <node value="211" /> <!-- This is the highest of the Group 3 --> 
     <node value="16" /> 
    </element> 
    <element order="2"> 
     <node value="32" /> 
     <node value="55" /> 
     <node value="77" /> 
     <node value="1" /> <!-- This is the lowest of the Group 3 --> 
    </element> 
    <element order="3"> 
     <node value="68" /> 
     <node value="74" /> 
     <node value="21" /> 
    </element> 
    <!-- Group 4 --> 
    ... 

</parent> 

希望這個問題不被廣泛提出。這些註釋不包含在XML中。

+0

您到目前爲止嘗試過什麼? –

+0

什麼都沒有:/我用foreach試過,但堅持要從每個組中檢索最小/最大值。我猜我應該爲每個while循環創建一個數組,並將值放入相應的數組中。 – g5wx

+0

隨着XML get的更新(基於一天的時間)訂單更改。例如:在凌晨0:00,它的order =「0」,但在早上6:00它被刪除,所以下一個節點變成第一個(order =「1」)。 – g5wx

回答

2

這是一種方法。

此解決方案僅適用於屬性是按例如1, 3, 4而不是例如3, 0, 4

$string = <<<XML 
<!-- XML data goes here --> 
XML; 

// suppress some errors 
libxml_use_internal_errors(true); 

$xml = simplexml_load_string($string); 

// step 1: group all the values 

// keep track of the previous order to determine when we have a new group 
// INF is just an arbitrarily large number to start the first group 
$prevOrder = INF; 
$groups = []; 
$i = -1; 

foreach ($xml->element as $element) { 
    $order = (int) $element->attributes()['order']; 
    // start a new group if the current order is smaller than the previous order 
    if ($order <= $prevOrder) { 
     $groups[++$i] = []; 
    } 
    // store the next values in that group 
    foreach ($element->node as $node) { 
     $groups[$i][] = (int) $node->attributes()['value']; 
    } 
    $prevOrder = $order; 
} 

// step 2: get the minimum and maximum of each group 

foreach ($groups as $group) { 
    printf('%d, %d<br>', min($group), max($group)); 
} 
+0

感謝Mikey,儘管我需要找到一種方法來創建第一個「組」,不管起始號碼是什麼,我會看看我是否可以使用您的解決方案並修改它 - upvoted。 – g5wx

+0

無論起始號碼如何,第一組都將始終創建。重要的是,每個組的訂單必須嚴格增加。 – Mikey

+0

我已經創建了另一個問題,其中XML有一個日期戳。如果你可以看看這種方式更容易,我會很感激,謝謝。 – g5wx