如何在php中使用simpleXML对xml属性进行排序?

zwghvu4y  于 2023-05-27  发布在  PHP
关注(0)|答案(1)|浏览(133)

我需要排序的xml属性之一,但我不知道如何。我尝试了函数sort,但它说它必须是字符串。我需要对tariffSpaceName进行排序,因为一个名称可以有多个数字,我希望将它们放在一起。我只显示一个订阅者,因为它的xml文件真的很大。

<?php
if (file_exists('2023-04-23-3622216414-5_13324_10_00_100005-s.xml')) {
    $xml = simplexml_load_file('2023-04-23-3622216414-5_13324_10_00_100005-s.xml');

?>
<html>
    <table>
        <tr>
            <th>tariffSpaceName</th>
            <th>serviceNumber</th>
            <th>coreProductNumber</th>
        </tr>

<?php
$count = count($xml->subscribers->subscriber);
for($aa=0; $aa<$count; $aa++) {

  foreach($xml->subscribers->subscriber[$aa]->attributes() as $key => $val) {
    if($key == "tariffSpaceName"){
sort($key);
      echo "<tr><td>[1]" . $val . "</td>";
    }

    if($key == "serviceNumber"){
      echo "<td>[2]" . $val . "</td>";
    }

    if($key == "summaryPriceWithTax"){
        echo "<td>[3]" . $val . "</td></tr>";
    }
  }

}
 echo "------<br>";
}else {
    exit('Failed to open test.xml.');
}

?>
</table>
</html>
<subscribers>
        <subscriber accountType="CUST" ownerRefNum="3622216414" ownerCustCode="" phoneNumber="601177068" customerAccountNumber="3622216414" tariffSpaceNumber="5900349478" tariffSpaceName="Böhmová Zdeňka" coreProductNumber="601177068" serviceNumber="601177068" parentServiceNumber="3622216414" serviceStatus="A" summaryPrice="8" summaryPriceTax="1.68" summaryPriceWithTax="9.68" mainTariff="Profil 2" serviceCategory="mobileVoice" noOfEdr="26">
mf98qq94

mf98qq941#

使用SimpleXML解析XML将返回文档元素。在您提供的示例中,这将是subscribers元素。
您可以使用iterator_to_array()(忽略键)将Traversable转换为数组,然后使用数组函数对数据进行排序/Map。
SimpleXML允许使用数组语法按名称寻址属性。这里没有必要重复它们。

$subscribersElement = new SimpleXMLElement(getXMLString());
// convert traversable into an array (ignoring keys)
$subscribers = iterator_to_array($subscribersElement->subscriber, false);

// sort using custom compare
usort(
    $subscribers,
    function(SimpleXMLElement $a, SimpleXMLElement $b) {
        return strnatcasecmp ($a['tariffSpaceName'], $b['tariffSpaceName']);
    }
);

// map and dump
var_dump(
    array_map(
        function(SimpleXMLElement $subscriber) {
          return [
           'tariffSpaceName' => (string)$subscriber['tariffSpaceName'],
           'serviceNumber' => (string)$subscriber['serviceNumber'],
           'summaryPriceWithTax' => (string)$subscriber['summaryPriceWithTax']
          ];
        },
        $subscribers
    )
);

function getXMLString() {
    return <<<'XML'
<subscribers>
    <subscriber accountType="CUST" ownerRefNum="3622216414" ownerCustCode="" phoneNumber="601177068" customerAccountNumber="3622216414" tariffSpaceNumber="5900349479" tariffSpaceName="Böhmová Zdeňka 2" coreProductNumber="601177068" serviceNumber="601177068" parentServiceNumber="3622216414" serviceStatus="A" summaryPrice="8" summaryPriceTax="1.68" summaryPriceWithTax="8.68" mainTariff="Profil 2" serviceCategory="mobileVoice" noOfEdr="26"/>
    <subscriber accountType="CUST" ownerRefNum="3622216414" ownerCustCode="" phoneNumber="601177068" customerAccountNumber="3622216414" tariffSpaceNumber="5900349478" tariffSpaceName="Böhmová Zdeňka" coreProductNumber="601177068" serviceNumber="601177068" parentServiceNumber="3622216414" serviceStatus="A" summaryPrice="8" summaryPriceTax="1.68" summaryPriceWithTax="9.68" mainTariff="Profil 2" serviceCategory="mobileVoice" noOfEdr="26"/>
</subscribers>
XML;
}

输出:

array(2) {
  [0]=>
  array(3) {
    ["tariffSpaceName"]=>
    string(17) "Böhmová Zdeňka"
    ["serviceNumber"]=>
    string(9) "601177068"
    ["summaryPriceWithTax"]=>
    string(4) "9.68"
  }
  [1]=>
  array(3) {
    ["tariffSpaceName"]=>
    string(19) "Böhmová Zdeňka 2"
    ["serviceNumber"]=>
    string(9) "601177068"
    ["summaryPriceWithTax"]=>
    string(4) "8.68"
  }
}

相关问题