0

I am trying to parse all currency values from XML file. I want to get this array from XML file.:

Array
(
    [USD] => 1.0501
    [RUB] => 0.0165
    [TRY] => 0.4244
)

But I got this:

Array
(
    [USD] => SimpleXMLElement Object
        (
            [0] => 1.0501
        )

    [RUB] => SimpleXMLElement Object
        (
            [0] => 0.0165
        )

    [TRY] => SimpleXMLElement Object
        (
            [0] => 0.4244
        )

)

And here is my php code:

<?php
@header('Content-Type: text/html; charset=utf-8;');
$date = date ("d.m.Y");
$cur_arr = array("TRY", "RUB", "USD");
$url = simplexml_load_file("http://cbar.az/currencies/" .$date.".xml");

foreach($url->ValType[1]->Valute as $key=>$value){
    $attr = $value->attributes();

    if(in_array($attr["Code"],$cur_arr)){
        $data[''.$attr["Code"].''] = $value->Value[0];
    }

}
print_r($data);
?>

I want to get an array like first example. I tried many codes but there is SimpleXMLElement Object on my result. How can I get it like first example?

1 Answers1

0

add (array) before value as

  foreach($url->ValType[1]->Valute as $key=>$value){
    $attr = $value->attributes();

    if(in_array($attr["Code"],$cur_arr)){
        $value = (array)$value->Value;
        $data[''.$attr["Code"].''] = $value[0];
    }

}

this will return

array(3) {
  ["USD"]=>
  string(6) "1.0501"
  ["RUB"]=>
  string(6) "0.0165"
  ["TRY"]=>
  string(6) "0.4244"
}
Osama Jetawe
  • 2,697
  • 6
  • 24
  • 40