【问题标题】:Why can't I loop through this SimpleXML Object?为什么我不能遍历这个 SimpleXML 对象?
【发布时间】:2012-09-27 07:26:14
【问题描述】:

我一直在努力解决这个问题,我真的希望我能得到一些帮助。提前致谢!

我有一个 SimpleXML 对象,它是这样的:

Object
(
|    SomeStuff Object
|    (
|    )
|    Entries Object
|    (
|    |    Entry Object
|    |    (
|    |    |    SomeMoreStuff Object
|    |    |    (
|    |    |    )
|    |    |    ImportantStuff Object
|    |    |    (
|    |    |    |    W1 Object
|    |    |    |    (
|    |    |    |    |    D1 Object
|    |    |    |    |    (
|    |    |    |    |    )
|    |    |    |    |    D2 Object
|    |    |    |    W2 Object
|    |    |    |    (
|    |    |    |    |    D1 Object
|    |    |    |    |    (
|    |    |    |    |    )
|    |    |    |    |    D2 Object

...等。 出于某种原因,使用以下代码会产生意想不到的结果:

foreach($xml->Entries->Entry->ImportantStuff as $key => $value) {
   echo "$key, $value";
}

只输出ImportantStuff,,而不是为每个D# 输出一行。在另一种情况下,我有一个对象,其中 ImportantStuff 的每个成员都具有相同的名称,在这种情况下,我使用 $xml->ImportantStuff->ImportantThing,它返回 all ImportantThings。在重要事物具有不同名称的情况下,我该如何模拟呢?

【问题讨论】:

  • 你能添加你的完整xml吗???
  • @Baba,我知道你不能echo 对象,但它是为了测试目的,以确保迭代确实有效。 :)
  • @Baba,我愿意,但已经回答了!谢谢,Geek Num 88

标签: php foreach simplexml


【解决方案1】:

使用 simplexml 你需要像这样进行 foreach

foreach($xml->Entries->Entry->ImportantStuff->children() as $key => $value) { echo "$key, $value"; }

【讨论】:

    猜你喜欢
    • 2017-05-23
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    相关资源
    最近更新 更多