【问题标题】:SimpleXMLElement get non-attribute valuesSimpleXMLElement 获取非属性值
【发布时间】:2017-03-30 10:15:21
【问题描述】:

我有以下 SimpleXMLElement:

object(SimpleXMLElement)#10 (3) { 
    ["@attributes"]=> array(3) { 
        ["id"]=> string(8) "18022352" 
        ["name"]=> string(14) "The Salmon Man" 
        ["slug"]=> string(14) "the-salmon-man" 
    } 

    ["bids"]=> object(SimpleXMLElement)#11 (1) { 
        ["price"]=> array(1) { 
                [0]=> object(SimpleXMLElement)#13 (1) { 
                        ["@attributes"]=> array(4) { 
                                ["decimal"]=> string(4) "9.60" 
                                ["percent"]=> string(5) "10.42" 
                                ["backers_stake"]=> string(5) "40.36" 
                                ["liability"]=> string(6) "347.00" 
                        } 
                 } 
        } 
    } 

    ["offers"]=> object(SimpleXMLElement)#12 (1) { 
        ["price"]=> array(1) { 
                [0]=> object(SimpleXMLElement)#15 (1) { 
                        ["@attributes"]=> array(4) { 
                                ["decimal"]=> string(4) "9.20" 
                                ["percent"]=> string(5) "10.87" 
                                ["backers_stake"]=> string(5) "85.35"         
                                ["liability"]=> string(5) "10.41" 
                        } 
                }  
        } 
    }
}

为什么会这样:

$horse[0]['name']

但这不是:

$horse[0]['bids'] // also tried $horse['bids'] and other ways

我可以得到如下值,但我希望搜索较小的对象:

$xml->xpath("//odds/event[@id='$matchid']/market[@slug='to-win']/contract[@name='$pony']/bids"); // $pony == $horse[0]['name']

【问题讨论】:

    标签: php xml-parsing simplexml


    【解决方案1】:

    查看 XML 本身通常比查看 SimpleXML 对象的print_r 更容易。在这种情况下,你有这样的事情:

    <horse id="18022352" name="The Salmon Man" slug="the-salmon-man">
        <bids>
             <price decimal="9.60" percent="10.42" backers_stake="40.36" liability="347.00" />
        </bids>
        <offers>
             <price decimal="9.60" percent="10.42" backers_stake="40.36" liability="347.00" />
        </offers>
    </horse>
    

    bids 项是一个元素,正如the SimpleXML Basic Usage guide 中所讨论的,您使用-&gt;foo 表示法访问子元素,而属性使用['foo'] 表示法。

    所以如果$horse 是那个&lt;horse&gt; 元素,你需要:

    $name = $horse['name'];
    $bids = $horse->bids;
    

    请注意,这等同于显式请求名为bidsfirst 孩子;无论实际上是否存在多个相同名称的元素,这两种形式都将起作用:

    $bids = $horse->bids[0];
    

    现在在实践中大概有一个或多个&lt;price&gt; 元素,因此您可能需要循环,然后回显每个decimal 属性。看起来像这样:

    foreach ( $horse->bids->price as $price ) {
        echo $price['decimal'];  
    }
    

    同样,这个循环只需要一个&lt;price&gt; 就可以正常工作,它只会循环一次。如果只有一个价格,或者你只关心第一个价格,你可以写:

    echo $horse->bids->price['decimal'];
    

    相当于:

    echo $horse->bids[0]->price[0]['decimal'];
    

    当然也可以:

    echo $horse->bids[0]->price['decimal'];
    echo $horse->bids->price[0]['decimal'];
    

    到达同一个地方的不同方式的数量是我不建议过分依赖print_r 输出的原因之一。另一个是它有时无法显示 XML 中的所有内容,这并不意味着如果您要求数据不可用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      相关资源
      最近更新 更多