PHP-从元素组中检索最低值和最高值
找不到正确的方法来执行以下操作:我有一个xml,其中包含元素,每个元素都有一个属性顺序,值范围为0-3。它们按该属性(0,1,2,3,0,1,2,3…)排序,并且是兄弟。许多元素可能会有所不同,文档中的第一个元素不必以0开头,因为XML每4小时刷新一次,每次刷新都会删除当前的第一个元素get,而行中的下一个元素将占据第一个位置。例如:在凌晨0:00,第一个元素具有属性order=“0”。在6:00,它被移除,下一个兄弟姐妹以属性order=“1”占据(第一)位置 我想为一组元素执行一个循环,其中元素的顺序从0到3(第一组元素的顺序可能为0、1、2或3除外),并检索每个组子节点的最低和最高值。例如:PHP-从元素组中检索最低值和最高值,php,xml,loops,Php,Xml,Loops,找不到正确的方法来执行以下操作:我有一个xml,其中包含元素,每个元素都有一个属性顺序,值范围为0-3。它们按该属性(0,1,2,3,0,1,2,3…)排序,并且是兄弟。许多元素可能会有所不同,文档中的第一个元素不必以0开头,因为XML每4小时刷新一次,每次刷新都会删除当前的第一个元素get,而行中的下一个元素将占据第一个位置。例如:在凌晨0:00,第一个元素具有属性order=“0”。在6:00,它被移除,下一个兄弟姐妹以属性order=“1”占据(第一)位置 我想为一组元素执行一个循环,其中
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>
剪裁底部结构时应打印:
11, 82
2, 92
1, 211
...
...
希望这个问题不要被广泛询问。注释不包含在XML中。这里有一种方法
此解决方案仅在属性排序时有效,例如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));
}
$string=
XML;
//抑制某些错误
libxml\u使用\u内部错误(true);
$xml=simplexml\u load\u string($string);
//步骤1:将所有值分组
//跟踪以前的订单,以确定何时有新的组
//INF只是一个任意大的数字,用于启动第一个组
$prevOrder=INF;
$groups=[];
$i=-1;
foreach($xml元素作为$element){
$order=(int)$element-attributes()['order'];
//如果当前订单小于上一订单,则启动新组
如果($order attributes()['value'];
}
$prevOrder=$order;
}
//步骤2:获取每组的最小值和最大值
foreach($组作为$组){
printf(“%d,%d
”,最小($group),最大($group));
}
这里有一种方法
此解决方案仅在属性排序时有效,例如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));
}
$string=
XML;
//抑制某些错误
libxml\u使用\u内部错误(true);
$xml=simplexml\u load\u string($string);
//步骤1:将所有值分组
//跟踪以前的订单,以确定何时有新的组
//INF只是一个任意大的数字,用于启动第一个组
$prevOrder=INF;
$groups=[];
$i=-1;
foreach($xml元素作为$element){
$order=(int)$element-attributes()['order'];
//如果当前订单小于上一订单,则启动新组
如果($order attributes()['value'];
}
$prevOrder=$order;
}
//步骤2:获取每组的最小值和最大值
foreach($组作为$组){
printf(“%d,%d
”,最小($group),最大($group));
}
到目前为止您尝试了什么?实际上什么都没有:/I我尝试了foreach,但坚持从每个组中检索最小/最大值。我猜我应该为每个while循环创建一个数组,并将值放入相应的数组中。随着XML的更新(基于一天中的时间)顺序会发生变化。例如:在上午0:00时,它的顺序为“0”,但在上午6:00时,它将被删除,因此下一个节点将成为第一个节点(顺序为“1”)。当您为每个节点创建时,您将位于各个节点(组)中?如果是这样,只需在循环外部创建一个变量,并将当前值与当前值进行比较。如果当前值大于或为空,则将其设置为当前值。最低值的原则相同。是否存在未显示的
级别,或者代码确实需要检查XML注释内部以确定哪些元素属于哪个组?Wh到目前为止,您尝试过吗?实际上什么都没有:/I我尝试过foreach,但坚持从每个组中检索最小/最大值。我猜我应该为每个while循环创建一个数组,并将值放入相应的数组中。随着XML的更新(基于一天中的时间)顺序会发生变化。例如:在上午0:00时,它的顺序为“0”,但在上午6:00时,它将被删除,因此下一个节点将成为第一个节点(顺序为“1”)。当您为每个节点创建时,您将位于各个节点(组)中?如果是这样,只需在循环外部立即创建一个变量,并将当前值与当前值进行比较。如果当前值大于或为空,则将其设置为当前值。最低值的原则相同。是否存在未显示的
级别,或者代码确实需要检查XML注释内部以确定哪些元素属于哪个组?Thanks Mikey,虽然我需要找到一种方法来创建第一个“组”无论起始编号是什么,我都将查看是否可以使用您的解决方案并对其进行修改-向上投票。无论起始编号是什么,都将始终创建第一个组。重要的是,每个组的订单都必须严格增加。我创建了另一个问题,其中xml有一个日期戳。请看一看如果这样更简单的话,我会非常感激的,谢谢。谢谢米奇,尽管我需要找到一种方法来创建第一个“组”无论起始编号是什么,我都将查看是否可以使用您的解决方案并对其进行修改-向上投票。无论起始编号是什么,都将始终创建第一个组。重要的是,每个组的订单都必须严格增加。我创建了另一个问题,其中xml有一个日期戳。请看一看如果这样比较容易的话,我会非常感激的,谢谢。