0

My script store data in a variable like this:

$dados = curl_exec($curl);
$dados = simplexml_load_string($dados);

and return this object:

object(SimpleXMLElement)[1]
  public 'cServico' => 
    object(SimpleXMLElement)[2]
      public 'Codigo' => string '40010' (length=5)
      public 'Valor' => string '28,35' (length=5)
      public 'PrazoEntrega' => string '1' (length=1)
      public 'ValorSemAdicionais' => string '26,10' (length=5)
      public 'ValorMaoPropria' => string '0,00' (length=4)
      public 'ValorAvisoRecebimento' => string '0,00' (length=4)
      public 'ValorValorDeclarado' => string '2,25' (length=4)
      public 'EntregaDomiciliar' => string 'S' (length=1)
      public 'EntregaSabado' => string 'S' (length=1)
      public 'Erro' => string '0' (length=1)
      public 'MsgErro' => 
        object(SimpleXMLElement)[3]

How can I store "valor" value in a variable without using a loop?

I try this with no sucess:

$foo = $dados[0]->cServico->valor;
marcelo2605
  • 2,734
  • 4
  • 29
  • 55
  • Please do a `print_r($dados)` so we can see what the object structure really is and add that to your question – RiggsFolly Oct 16 '15 at 17:23
  • Possible duplicate of http://stackoverflow.com/questions/2867575/get-value-from-simplexmlelement-object ... Trying something like this may lead to a solution: $foo = (string) $dados[0]->cServico->valor; – Kristoffer Bohmann Oct 16 '15 at 19:14

1 Answers1

0

The problem is that you 'key' is upper camelcase, you're trying to access ->valor but your key is ->Valor.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Felipe Umpierre
  • 188
  • 2
  • 12