Get values from within stdClass array stdClass

Asked

Viewed 2,871 times

2

I have this class where I can query and return data from an external webservice:

$params = array(
    'usuario' => $usuario,
    'senha' => $senha,
    'quantidade' => 2
);

$wsdl = 'http://xxxx.com?WSDL';

$options = array(
        'uri'=>'http://schemas.xmlsoap.org/soap/envelope/',
        'style'=>SOAP_RPC,
        'use'=>SOAP_ENCODED,
        'soap_version'=>SOAP_1_1,
        'cache_wsdl'=>WSDL_CACHE_NONE,
        'connection_timeout'=>15,
        'trace'=>true,
        'encoding'=>'UTF-8',
        'exceptions'=>true,
    );
try {
    $soap = new SoapClient($wsdl, $options);        
    $data = $soap->obterDados($params);     
}
    catch(Exception $e) {
    die($e->getMessage());
}

echo "<pre>";
var_dump($data);
echo "</pre>";

But the result that the var_dump($data) It returns me something like this:

object(stdClass)#2 (1) {
  ["return"]=>
  array(2) {
    [0]=>
    object(stdClass)#3 (26) {
      ["idVeiculo"]=>
      int(123456)
      ["placa"]=>
      string(9) "ABC2222-2"      
    }
    [1]=>
    object(stdClass)#4 (26) {
      ["idVeiculo"]=>
      int(123457)
      ["placa"]=>
      string(9) "ABC1111-2"      
    }
  }
}

I need to get the data ["idVeiculo"] and ["placa"], but the examples do not contain the structure with 2 objects stdClass and an array return.

Someone can help me?

EDIT

I added the code below to the original and was able to visualize the plates. There is a way less paleactive than this?

$xml = json_decode(json_encode($data),true);

echo $xml["return"][0]["placa"];
echo $xml["return"][1]["placa"];
  • 2

    try this: foreach($data->Return as $veiculo){ echo $veiculo->placa; }

  • 2

    @Derleilisboa. It worked perfectly!

  • 2

    then I draw up a complete response and put as a response, to follow the pattern of the site that does not recommend responses in the comments.

  • 1

    It took a little longer to publish the answer ;)

1 answer

1


By your example we have the following structure:

The object $data where it has an attribute called return which is an object array. To access the attribute return we use $data->return.

By the fact of return be an array we can run through it dynamically using the foreach($data->return, $veiculo) where the first parameter is the array to be traversed (in our case the $data->return) and the second is the element returned in each iteration(We give this parameter the name we want, in this case I found it convenient to name $veiculo because at each iteration it will return an object with vehicle data).

Based on the above explanation the code to solve your problem is as follows:

foreach ($data->return as $veiculo) { 
    echo $veiculo->placa; 
}
  • 1

    Excellent explanation. It worked perfectly. And in case I have a list of objects Events inside $veiculo just do foreach($veiculo->evento as $evento){ echo $evento->codigo; } inside the previous foreach, correct?

  • 1

    That’s right, within each iteration of foreach previous you can go through this array $veiculo->evento, and manipulate your data as needed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.