foreach和simplexml

时间:2012-08-28 07:31:54

标签: php xml simplexml

这是我的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

我该怎么办?

感谢。

5 个答案:

答案 0 :(得分:5)

这实际上是一个更基本的例子:

$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

你应该看一下手册中的简单例外部分,它有一些非常有见地的例子,这些例子比我的更深入解释:

我希望无论如何答案都有帮助。相关问题是:

答案 1 :(得分:4)

如果您更习惯于数组,也可以将简单的xml翻转为数组:s

<?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;
}

答案 2 :(得分:0)

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

答案 3 :(得分:0)

如果您只想循环遍历行:

<?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>";
}

?>

答案 4 :(得分:0)

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

var_dump($newArray);