【问题标题】:PHP - retrieve lowest and highest values from elements groupsPHP - 从元素组中检索最低和最高值
【发布时间】:2017-04-17 16:21:58
【问题描述】:

找不到正确的方法来执行以下操作:我有 xml,其中包含元素,其中每个元素都有一个属性顺序,值范围为 0-3。它们按该属性 (0,1,2,3,0,1,2,3...) 排序,并且是兄弟姐妹。许多元素可能会有所不同,并且文档中的第一个元素不必以 0 开头,因为 XML 每 4 小时刷新一次,并且每次刷新当前的第一个元素都会被删除,并且行中的下一个元素会超过第一个位置.例如:在上午 0:00,第一个元素的属性 order="0"。在 6:00,它被移除,下一个兄弟以属性 order="1" 占据他的(第一)位置。

我想为一组从 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>

希望这个问题不会被广泛提出。 cmets 不包含在 XML 中。

【问题讨论】:

  • 请问您到目前为止都尝试过什么?
  • 实际上什么都没有:/我尝试过使用 foreach,但坚持从每个组中检索最小/最大值。我猜我应该为每个 while 循环创建一个数组,并将值放入相应的数组中。
  • 随着 XML 的更新(基于一天中的时间),订单会发生变化。例如:在上午 0:00 它有 order="0",但在上午 6:00 它被删除,因此下一个节点成为第一个节点 (order="1")。
  • 当你为每个你在单个节点(组)内?如果是这样,只需在 THAT 循环之外立即创建一个变量并将当前值与该值进行比较。如果它更大或为空,请将其设置为当前值。最低的原则相同。
  • 您是否没有显示&lt;group&gt; 级别,或者代码是否确实需要检查内部 XML cmets 以确定哪些元素属于哪个组?

标签: php xml loops


【解决方案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));
}

【讨论】:

  • 谢谢 Mikey,尽管我需要找到一种方法来创建第一个“组”,而不管起始编号是什么,我会看看我是否可以使用您的解决方案并对其进行修改 - 赞成。跨度>
  • 无论起始编号如何,都会始终创建第一个组。重要的是每个组的订单必须严格增加。
  • 我创建了另一个问题,其中 xml 有日期戳。如果您能看看这样是否更容易,我将不胜感激,谢谢。
猜你喜欢
  • 2016-01-16
  • 2015-10-21
  • 2018-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-04
相关资源
最近更新 更多