如何通过单词(键)匹配而不是 PHP 中的索引号来获取 XML 值?

How to get XML values by word (key) match instead of by index number in PHP?

我有 xml 个这样的文件:

<?xml version="1.0" encoding="utf-8"?>
<products>
    <product ID="12345">
        <name>Sunny Beach Resort</name>
        <price currency="EUR">439.00</price>
        <URL>https://www.example.com/</URL>
        <images>
            <image>https://www.example.com/images/image.jpg</image>
        </images>
        <properties>
            <property name="country">
                <value>Spanje</value>
            </property>
            <property name="region">
                <value>Gran Canaria</value>
            </property>
            <property name="cityURL">
                <value>https://www.example.com/</value>
            </property>
            <property name="usp">
                <value>Adults only 16+</value>
                <value>Een rustpunt op een centrale plek</value>
                <value>Lichte kamers met ruimtelijke sfeer</value>
            </property>
            <property name="surrounding">
                <value>afstand tot strand circa 1 kilometer (zandstrand)</value>
                <value>afstand tot centrum: circa 1 kilometer</value>
                <value>afstand tot Barstreet circa 500 meter</value>
                <value>afstand tot luchthaven circa 30 kilometer</value>
            </property>
            <property name="serviceType">
                <value>Logies ontbijt</value>
            </property>
        </properties>
        <variations>
            <variation>
                <property name="roomType">
                    <value>2-persoonskamer luxe type voor alleengebruik</value>
                </property>
                <property name="roomOccupation">
                    <value>geschikt voor 1 persoon</value>
                </property>
            </variation>
            <variation>
                <property name="roomType">
                    <value>2-persoonskamer luxe type standaard</value>
                </property>
                <property name="roomOccupation">
                    <value>geschikt voor 2 tot 3 personen</value>
                </property>
            </variation>
        </variations>
    </product>

名称、url 和图像是在 php 中提取的非常可靠的值。 但是当涉及到他们放在属性甚至变体中的条目时,它变得非常不可靠。 因为有时他们会更改数据条目的顺序。或者他们遗漏了一项。

所以下面的代码今天有效,但明天可能不行,因为索引号改变了:

$xml = simplexml_load_file($xml_folder.$feed_file);
foreach($xml->product as $product) {
$country = $xml->product[$index]->properties->property[0]->value;
$region= $xml->product[$index]->properties->property[1]->value;
$servicetype = $xml->product[$index]->properties->property[3]->value;
$facilities = $xml->product[$index]->properties->property[13]->value;
$transporttype = $xml->product[$index]->variations->variation->property[4]->value;

// more code to put the data into html code
}

有没有办法通过标签中的名称而不是索引号来获取值? 所以像这样:

$servicetype = $xml->product[$index]->properties->property[name="serviceType"]->value;

您可以遍历 property 详细信息并查看您拥有的内容。您也不需要 foreach 中的 $xml->product,它返回给父级。

$xml = new simplexmlelement($xml);
foreach($xml->product as $product) {
    foreach($product->properties->property as $property) {
        if($property['name'] == 'serviceType'){
            echo 'Service type is' . $property->value . ' for ' . $product ->name;
        }
    }
}

演示:https://3v4l.org/bQLUa

您可以使用 XPath 生成单个项目,例如

$country = $product->xpath("./properties/property[@name='country']/value")[0];

或者您可以在遍历属性时构建所有属性的列表并创建关联数组...

foreach($xml->product as $index => $product) {
    $details = [];
    foreach ( $product->properties->property as $property ){
        $details[(string)$property['name']] = (string)$property->value;
    }
    print_r($details);

给出类似

的东西
Array
(
    [country] => Spanje
    [region] => Gran Canaria
    [cityURL] => https://www.example.com/
    [serviceType] => Logies ontbijt
)

第二种方法(我认为)更有效,因为它 1 传递数据,必须为您需要的每个元素完成 XPath。

如果你有一些细节的倍数...

foreach($xml->product as $index => $product) {
    $details = [];
    foreach ( $product->properties->property as $property ){
        $type = (string)$property['name'];
        $values = [];
        foreach ( $property->value as $value )  {
            $values[] = (string)$value;
        }

        if ( is_array($values) )   {
            $details[$type] = $values;
        }
        else    {
            $details[$type] = $values[0];
        }
    }
    print_r($details);
}

会给出类似

的东西
Array
(
    [country] => Array
        (
            [0] => Spanje
        )

    [region] => Array
        (
            [0] => Gran Canaria
        )

    [cityURL] => Array
        (
            [0] => https://www.example.com/
        )

    [usp] => Array
        (
            [0] => Adults only 16+
            [1] => Een rustpunt op een centrale plek
            [2] => Lichte kamers met ruimtelijke sfeer
        )

    [surrounding] => Array
        (
            [0] => afstand tot strand circa 1 kilometer (zandstrand)
            [1] => afstand tot centrum: circa 1 kilometer
            [2] => afstand tot Barstreet circa 500 meter
            [3] => afstand tot luchthaven circa 30 kilometer
        )

    [serviceType] => Array
        (
            [0] => Logies ontbijt
        )

)

我想要与其他答案类似的东西,但使用 xpath:

$properties = $xml->xpath("//products//property");
foreach ($properties as $property) {
    echo  $property->attributes(). ": " . $property->value. "<br>";}

输出:

country: Spanje
region: Gran Canaria
cityURL: https://www.example.com/
serviceType: Logies ontbijt
roomType: 2-persoonskamer luxe type voor alleengebruik
roomOccupation: geschikt voor 1 persoon
roomType: 2-persoonskamer luxe type standaard
roomOccupation: geschikt voor 2 tot 3 personen