2017-04-17 21 views
0

適切な方法を見つけることができません。xmlには、それぞれが属性の順序と0-3の範囲の値を持つ要素が含まれています。それらはその属性(0,1,2,3,0,1,2,3 ...)によって順序付けされ、兄弟です。いくつかの要素が異なる可能性があり、XMLの4時間ごとにリフレッシュされ、リフレッシュされるたびに現在の最初の要素getが削除され、次の要素が1番目の位置を占めるため、文書の最初の要素が0で始まる必要はありません。たとえば、午前0時に、最初の要素は属性order = "0"を持ちます。 6時には、それは取り除かれ、次の兄弟は、属性order = "1"の彼(最初)の場所にいます。PHP - 要素グループから最低値と最高値を取得します。

最初の要素が0,1,2、または3の順序を持​​つ可能性がある最初のグループを除いて、0から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時には、次のノードが最初になるように削除されます(order = "1")。 – g5wx

答えて

2

ここには1つの方法があります。

この解決策は、属性が順序付けされている場合にのみ機能します。 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

ありがとうマイキー、私はあなたが最初の "グループ"を作成する方法を見つける必要がありますが、開始番号が何であるか、私はあなたのソリューションを使用してそれを変更することができますか - upvoted表示されます。 – g5wx

+0

最初のグループは開始番号に関係なく常に作成されます。重要なことは、グループごとに注文が厳しく増加しなければならないことです。 – Mikey

+0

私はxmlに日付スタンプがある別の質問を作成しました。この方法が簡単かどうかを調べることができれば、感謝します。 – g5wx

関連する問題