I need to access data from the Federal Reserve (Interest rates, etc). They offer an API in PHP, and although I know some PHP and XML, I have never seen anything like this.
I already read a bunch of tutorials about simple XML, etc, but their examples are never as complex as this XMl seems to be.
See the example below. How to navigate in PHP to (for example) the last value "3.25"? I am confused with the mix of objects and arrays. Thanks !
I am trying this:
($observations has the XML string)
$observations = $series_api->observations($parameters);
(this I how I am trying to reach the value I want... it doesn't work)
foreach ( $observations->observation[0]->{@attributes}[value] as $value )
{
echo "Value ".$value."<br>" ;
}
/* this is what is inside $observations*/
SimpleXMLElement Object (
[@attributes] => Array (
[realtime_start] => 2016-07-08
[realtime_end] => 2016-07-08
[observation_start] => 1600-01-01
[observation_end] => 9999-12-31
[units] => lin
[output_type] => 1
[file_type] => xml
[order_by] => observation_date
[sort_order] => asc
[count] => 15895
[offset] => 0
[limit] => 100000
)
[observation] => Array (
[0] => SimpleXMLElement Object (
[@attributes] => Array (
[realtime_start] => 2016-07-08
[realtime_end] => 2016-07-08
[date] => 1955-08-04
[value] => 3.25
)
)
[1] => SimpleXMLElement Object ( [@attributes] => Array ( [realtime_start] => 2016-07-08 [realtime_end] => 2016-07-08 [date] => 1955-08-05 [value] => 3.25 ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [realtime_start] => 2016-07-08 [realtime_end] => 2016-07-08 [date] => 1955-08-08 [value] => 3.25 ) ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [realtime_start] => 2016-07-08 [realtime_end] => 2016-07-08 [date] => 1955-08-09 [value] => 3.25 ) ) [4] => SimpleXMLElement Object ( [@attributes] => Array ( [realtime_start] => 2016-07-08 [realtime_end] => 2016-07-08 [date] => 1955-08-10 [value] => 3.25 ) ) [5] => SimpleXMLElement Object ( [@attributes] => Array ( [realtime_start] => 2016-07-08 [realtime_end] => 2016-07-08 [date] => 1955-08-11 [value] => 3.25 ) )
" ; } – Alex D Jul 08 '16 at 20:46
" ; } – Alex D Jul 08 '16 at 21:57