这是一个简单的循环,但我不知道怎么做


Its a simple loop but I do not know how to do it

我需要解析以下xml,但当我使用代码时,它只读取前p个标记,我知道这是一个简单的循环,但我很困惑。

 <s>
 <j u="here1"/>
   <p v="here1_1"/>
   <p v="here1_2"/>
   <p v="here1_3"/>
   <p v="here1_4"/>
   <p v="here1_5"/>
   <p v="here1_6"/>
</s>
<s>
 <j u="here2" />
   <p v="here2_1"/>
   <p v="here2_2"/>
   <p v="here2_3"/>
   <p v="here2_4"/>
   <p v="here2_5"/>
   <p v="here2_6"/>
</s>

分枝杆菌

$xml = simplexml_load_string($myXml);
foreach ($xml->s as $tags) {
        echo $tags->j->attributes()->u . " ";
        echo $tags->p->attributes()->v . " ";
    }

结果是>>此处1此处1_1此处2此处2_1

但是结果应该是here1 here1_1。。。。。here1_6 here2 here2_1。。。。。此处2_6

也许是这样的?

$xml = simplexml_load_string($myXml);
foreach ($xml->s as $tags) foreach ($tags as $tag) {
    echo $tag->attributes()->u . " "; // $tag['u'] you can access attributes like this too
    echo $tag->attributes()->v . " "; // $tag['v']
}

这应该会给你

here1
here1_1 
here1_2 
here1_3 
here1_4 
here1_5 
here1_6 
here2 
here2_1 
here2_2 
here2_3 
here2_4 
here2_5 
here2_6 

由于有多个p标签,您需要循环通过p标签

$xml = simplexml_load_string($myXml);
foreach ($xml->s as $tags) {
        echo $tags->j->attributes()->u . " ";
        foreach($tags->p as $ptags){
            echo $ptags->attributes()->v . " ";
        }
    }