Satch3000
Satch3000

Reputation: 49384

PHP Echo array values loop (no data displayed)

I have an array ... Here is the structure / data:

array(1) { 
    [0]=> object(SimpleXMLElement)#1 (18) 
        { 
            ["data_123"]=> object(SimpleXMLElement)#3 (29) 

            { 
                ["field1"]=> string(7) "123" 
                ["field2"]=> string(2) "10" 
                ["field3"]=> string(19) "2013-03-05 17:00:00" 
                ["field4"]=> string(19) "2013-03-05 18:00:00" 

            } 

                ["data_234"]=> object(SimpleXMLElement)#4 (29) 

            { 

                ["field1"]=> string(7) "234" 
                ["field2"]=> string(2) "10" 
                ["field3"]=> string(19) "2013-03-05 17:40:00" 
                ["field4"]=> string(19) "2013-03-05 18:10:00" 

            } 

        } 

    }

I am trying to create a loop to display the data but nothing is showing up:

foreach ($result as $key => $list) {
   echo "key.: " . $key . "\n";
   echo "field1: " . $list['field1'] . "\n";
   echo "field2: " . $list['field2'] . "\n";
}

It's just not returning any data.

I'm guessing that the loop might be wrong for this array structure?

How can I get the data echoed for this array?

Upvotes: 0

Views: 174

Answers (1)

AD7six
AD7six

Reputation: 66188

$list is an array of objects so you need two loops and appropriate syntax. e.g.:

foreach($list as $objects) {
    foreach($objects as $key => $obj) {
        echo "key.: " . $key . "\n";
        echo $obj->field1 . "\n";
        echo $obj->field2 . "\n";
        echo $obj->field3 . "\n";
        echo $obj->field4 . "\n";
    }
}

Upvotes: 2

Related Questions