Ryan
Ryan

Reputation: 10049

PHP: echo a value from simplexmloelement object

This is my code:

$xml2  = (array) simplexml_load_string($xml_file);

Using print_r this is my array:

 Array
(
  [program] => Array
        (
        [0] => SimpleXMLElement Object
        (
            [date] => asgasg sgasgasg
            [start_time] => asdg asg
            [leadtext] => as asdgsagsdgasgasgd
            [name] => as gsadgasg
            [bline] => sag asdg
            [synopsis] => asg asga sdg
            [url] => asg sdgasgasg
        )

        [1] => SimpleXMLElement Object
        (
            [date] => sgasgasg1
            [start_time] => asg1
            [leadtext] => as1
            [name] => gsadgasg1
            [bline] => asdg1
            [synopsis] => sdg1
            [url] =>sdgasgasg1
        )

        )
)

how can I do a echo to get the contents of [date] from the 2nd SimpleXMLElement (which is sgasgasg1 in the above example)

Upvotes: 0

Views: 160

Answers (2)

You can do it like this:

echo $xml2['program'][1]->date;

Upvotes: 2

GBD
GBD

Reputation: 15981

Use

foreach($xml2['program'] as $key=>$value) {

   echo $value->date;

}

As you told in your comment only

echo $xml2['program'][1]->date;

Upvotes: 2

Related Questions