PHP-从元素组检索最低和最高值 [英] PHP - retrieve lowest and highest values from elements groups
问题描述
找不到正确的方法来执行以下操作:我有xml,其中包含每个元素都有一个属性顺序且值的范围为0-3的元素.它们由该属性(0,1,2,3,0,1,2,3 ...)进行排序,并且是同级.许多元素可以变化,并且文档中的第一个元素不必以0开头,因为XML每4小时刷新一次,并且每次刷新时,当前的第一个元素都将被删除,而行中的下一个元素则位于第一个位置.例如:上午0:00,第一个元素的属性为order ="0".在6:00,它被删除,下一个兄弟姐妹以属性order ="1"的身份成为他(第一个)位置.
Can't find a proper way to do the following: i have xml which contains elements where each one has a attribute order and the value ranging from 0-3. They are ordered by that attribute (0,1,2,3,0,1,2,3...) and are siblings. A number of the elements can vary and it's not necessary that the first one in the document starts with 0 as the XML is refreshed every 4 hours and with every refresh the current first element get's deleted and the next element in line take's over the first position. For example: at 0:00 am the first element has attribute order="0". At 6:00 it gets removed and the next sibling takes he's (first) place with attribute order="1".
我想对一组元素按从0到3的顺序进行循环(第一个元素除外,第一个元素的顺序可能为0、1、2或3),并检索最低和最高值每个组子节点的数量.例如:
I would like to do a loop for a group of elements where they are ordered from 0-3 (except the first group where the first element may have order 0, 1, 2, or 3) and retrieve lowest and highest values of each group child nodes. For example:
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中.
Hope the question is not to widely asked. The comments are not included in the XML.
推荐答案
这是一种方法.
仅当属性按顺序排列时,此解决方案才有效1, 3, 4
,而不是3, 0, 4
.
This solution will only work if the attributes are ordered e.g. 1, 3, 4
and not e.g. 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));
}
这篇关于PHP-从元素组检索最低和最高值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!