如何为多维数组键获得一个foreach循环

时间:2022-06-15 21:37:09
    <user>
      <name>
         <dob><test>15</test>  </dob>
         <age></age>
    </name>
        <name1><test></test></name1>
    <name2></name2>
    </user>

Explain : i have need output like( user.name.dob user.name.age user.name1.test user.name2) this.i want use one for each .Can i get by recursion method.if yes that how.please help me out this

说明:我需要输出像(user.name.dob user.name.age user.name1.test user.name2)this.i想要每个使用一个。可以通过递归方法获得。如果是,how.please帮助我这个

1 个解决方案

#1


2  

Here's a simple example, you'll want to restructure XML a bit.

这是一个简单的例子,你需要稍微重构一下XML。

$xml = new SimpleXMLElement('<parent><user>
      <name>John</name>
      <dob>1/1/2015</dob>
      <age>12</age>
    </user>
    <user>
      <name>luke</name>
      <dob>2/1/2015</dob>
      <age>21</age>
    </user></parent>');
$base = 0;
foreach ($xml as $user) {
    $base++;
    echo  $base . '.user.' . $user->name . '.' . $user->dob . '.' . $user->age . "\n";
}

Link to the manual, http://php.net/manual/en/simplexmlelement.children.php.

链接到手册,http://php.net/manual/en/simplexmlelement.children.php。

#1


2  

Here's a simple example, you'll want to restructure XML a bit.

这是一个简单的例子,你需要稍微重构一下XML。

$xml = new SimpleXMLElement('<parent><user>
      <name>John</name>
      <dob>1/1/2015</dob>
      <age>12</age>
    </user>
    <user>
      <name>luke</name>
      <dob>2/1/2015</dob>
      <age>21</age>
    </user></parent>');
$base = 0;
foreach ($xml as $user) {
    $base++;
    echo  $base . '.user.' . $user->name . '.' . $user->dob . '.' . $user->age . "\n";
}

Link to the manual, http://php.net/manual/en/simplexmlelement.children.php.

链接到手册,http://php.net/manual/en/simplexmlelement.children.php。