Search code examples
phpxml-parsingparsexml

simplexml_load_string doesn't parse right


So, I'm trying to parse and xml and get some values out of it:

    $xml = simplexml_load_string(file_get_contents('http://www.bnr.ro/nbrfxrates.xml'));
    $currency = [];

    foreach($xml->Body->Cube->Rate as $rate)
    {
        $currency[] = [
            "name" => $rate["currency"],
            "value" => $rate,
            "multiplier" => $rate["multiplier"]
        ];
    }

    return $currency;

My $rate variable should be the value inside the Rate tag (Ex: 1.0806) instead it gives me this:

object(SimpleXMLElement)[110]
   public '@attributes' => 
      array (size=1)
         'currency' => string 'AED' (length=3)

Solution

  • Converting $rate to (string) will work:

        $currency[] = [
            "name" => $rate["currency"],
            "value" => (string)$rate,
            "multiplier" => $rate["multiplier"]
        ];