0

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?

2
  • You have array of arrays. Nest foreach into existing or use foreach ($result[0] as $key => $list) Elso enable notices in dev environment, so you could see Undefined index notice. Commented Mar 5, 2013 at 18:14
  • I added foreach ($result[0] as $key => $list) as suggested and it's now showing the key but not field1, field2, field3 or field4 Commented Mar 5, 2013 at 18:20

1 Answer 1

2

$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";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.