Get values from within stdClass array stdClass

1

I have this class where I can do the 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 var_dump($data) returns me is 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"] , however the cases exemplified do not contain the structure with 2 stdClass objects and a return array.

Can anyone help me?

EDIT

I added the code below the original and I was able to see the plates. Is there a less palatable form than this?

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

echo $xml["return"][0]["placa"];
echo $xml["return"][1]["placa"];
    
asked by anonymous 28.06.2017 / 20:39

1 answer

1

By your example we have the following structure:

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

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

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

foreach ($data->return as $veiculo) { 
    echo $veiculo->placa; 
}
    
30.06.2017 / 04:10