foreach and 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.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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:

Solution 2

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
        (
            
=> Array ( [0] => Array (
=> Array ( [0] => 1 [1] => David [2] => Johnson ) ) [1] => Array (
=> 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
        (
            
=> Array ( [0] => 1 [1] => David [2] => Johnson ) ) [1] => Array (
=> 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; }

Solution 3

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

Solution 4

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

?>

Solution 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);

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply