What is the elegant way to read this PHP Object?

4

I have Result of a Webservice below, I'm doing some foreachs to read each level of the class, but I think you should have a more elegant way of reading and getting the values of the object, follow Result :

stdClass Object
(
    [Codigo] => 1-0-21
    [Descricao] => REGIONAL DDD21
    [DddArray] => stdClass Object
        (
            [string] => 21
        )

    [Valores] => stdClass Object
        (
            [Valor] => Array
                (
                    [0] => stdClass Object
                        (
                            [ValorFace] => 1000
                            [ValorBonus] => 0
                            [Produto] => RECARGA CELULAR
                        )

                    [1] => stdClass Object
                        (
                            [ValorFace] => 1500
                            [ValorBonus] => 0
                            [Produto] => RECARGA CELULAR
                        )

                    [2] => stdClass Object
                        (
                            [ValorFace] => 2000
                            [ValorBonus] => 0
                            [Produto] => RECARGA CELULAR
                        )


                )

        )

)

This text is the result of the function:

$res = $client->RetornaRegionaisPorOperadoraDdd($params);

The way I'm doing is very "POG":

foreach ($res as $key => $value){
    foreach ($value as $keyopera => $valopera){
        print_r($valopera);
    }
}
    
asked by anonymous 29.01.2016 / 12:58

1 answer

7

The simplest way is to indicate which key you want to iterate, thus it chooses the second foreach.

$res = $client->RetornaRegionaisPorOperadoraDdd($params);
foreach ($res->valores->valor as $item){
   echo $item['Produto'] .' - '. $item['ValorFace'];
}
    
29.01.2016 / 13:04