的foreach和SimpleXML(foreach and simplexml)

2019-06-27 10:58发布

这是我的xml文件:

<?xml version="1.0" encoding="utf-8" ?> 
<documentElement>
    <rows>
        <row>
            <column>1</column>
            <column>David</column>
            <column>Johnson</column>
        </row>
        <row>
            <column>2</column>
            <column>Jack</column>
            <column>Nixon</column>
        </row>
    </rows>
</documentElement>

我要循环行并将它们添加到一个数组。 我通过它simplexml通过这些对象,然后循环,但不能做到这一点:

$xml->rows
$xml->rows->row
$xml->row
$xml->column

我该怎么办?

谢谢。

Answer 1:

这实际上是比较基本的例子之一:

$sxml = simplexml_load_string($xml);

foreach($sxml->rows->row as $name => $row)
{
    echo $name, ': ', var_dump($row);
}

它会给你以下的输出:

row: object(SimpleXMLElement)#3 (1) {
  ["column"]=>
  array(3) {
    [0]=>
    string(1) "1"
    [1]=>
    string(5) "David"
    [2]=>
    string(7) "Johnson"
  }
}
row: object(SimpleXMLElement)#5 (1) {
  ["column"]=>
  array(3) {
    [0]=>
    string(1) "2"
    [1]=>
    string(4) "Jack"
    [2]=>
    string(5) "Nixon"
  }

你可以在这里找到一个在线演示: http://codepad.org/CYyQhvnM

实际上,你应该看看手册中的简单exmamples部分,它具有的深度比我在这里解释的多一些很有见地的例子:

  • 基本使用的SimpleXML

我希望答案是有帮助的反正。 相关的问题是:

  • 如何使用的foreach使用PHP和XML(simplexml的)


Answer 2:

S:您还可以,如果你更习惯阵列翻转简单的XML到一个数组

<?php
$xml = simplexml_load_file('./your.xml');
$json = json_encode($xml);
$array = json_decode($json,TRUE);
print_r($array);
/**
 *Array
(
    [rows] => Array
        (
            [row] => Array
                (
                    [0] => Array
                        (
                            [column] => Array
                                (
                                    [0] => 1
                                    [1] => David
                                    [2] => Johnson
                                )

                        )

                    [1] => Array
                        (
                            [column] => Array
                                (
                                    [0] => 2
                                    [1] => Jack
                                    [2] => Nixon
                                )

                        )

                )

        )

)
 */

foreach($array['rows']['row'] as $row){
    echo 'Id:'.$row['column'][0],
         ' Firstname:'.$row['column'][1],
         ' Lastname:'.$row['column'][2].'<br />'.PHP_EOL;
}

/*Result
Id:1 Firstname:David Lastname:Johnson<br />
Id:2 Firstname:Jack Lastname:Nixon<br />
*/
?>

或者成为一个更清洁例如通过hakra

<?php
$xml = simplexml_load_file('./test.xml');
$rows = json_decode(json_encode(iterator_to_array($xml->rows->row, 0)), 1);
print_r($rows);
/**
 *
Array
(
    [0] => Array
        (
            [column] => Array
                (
                    [0] => 1
                    [1] => David
                    [2] => Johnson
                )

        )

    [1] => Array
        (
            [column] => Array
                (
                    [0] => 2
                    [1] => Jack
                    [2] => Nixon
                )

        )

)
 */

foreach($rows as $row){
    echo 'Id:'.$row['column'][0],
         ' Firstname:'.$row['column'][1],
         ' Lastname:'.$row['column'][2].'<br />'.PHP_EOL;
}


Answer 3:

尝试$xml->rows[0]->row[0]



Answer 4:

如果你只是想通过行中循环:

<?php

$xmlstr = <<<XML
<?xml version="1.0" encoding="utf-8" ?> 
<documentElement>
    <rows>
        <row>
            <column>1</column>
            <column>David</column>
            <column>Johnson</column>
        </row>
        <row>
            <column>2</column>
            <column>Jack</column>
            <column>Nixon</column>
        </row> 
    </rows>
</documentElement>
XML;

$element = new SimpleXMLElement($xmlstr);

foreach($element->rows->row as $row) {

    foreach($row as $column) {
        echo "{$column}<br>";                
    }

    echo "<hr>";
}

?>


Answer 5:

$sxml = simplexml_load_string($xml);
$newArray = array();
foreach($sxml->rows as $num => $row){
    foreach($row as $el){
        (array)$newArray[] = (array)$el;
    }
}

var_dump($newArray);


文章来源: foreach and simplexml