foreach和simplexml

It's my xml file:

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

I want to loop over rows and add them to an array. I passed it to simplexml and then loop through these objects, but couldn't do it:

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

What should I do?

Thanks.

This is actually one of the more basic examples:

$sxml = simplexml_load_string($xml);

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

Which will give you the following output:

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

You can find an online-demo here: http://codepad.org/CYyQhvnM

You should actually take a look at the simple exmamples section in the manual, it has some very insightful examples that are explained more in depth than mine here:

I hope the answer is helpful anyway. Related questions are:

Try $xml->rows[0]->row[0]

You can also flip simple xml to an array if your more used to arrays :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 />
*/
?>

Or an even cleaner example by 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;
}

If you simply want to loop through the rows:

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

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

var_dump($newArray);