Get data inside stdClass with several stdClass inside each other in PHP

Asked

Viewed 217 times

2

I have as answer this stdClass:

stdClass Object ( [Search_AdvancedResult] => stdClass Object ( [ErrorMessage] => [MaxResults] => 1 [NumObjects] => 1 [QueryID] => 242122 [TotalResults] => 1 [Objects] => stdClass Object ( [RMWSObject2] => stdClass Object ( [ObjectID] => 549012 [EntryDate] => 2016-02-22 16:33:47 [EntryUserName] => de0210598 [ImportDate] => 2016-02-22 16:38:07 [CaptureDate] => 2016-02-22 16:33:00 [CaptureLocation] => DIRECIONAL [ClassCode] => SP1 [ClassName] => ADM Obra - Pagamentos por SP [SourceCode] => DIRECIONAL [SourceName] => Obras [TypeCode] => 311C [TypeName] => 311C (Central de Notas) [StateCode] => [StateName] => [FileType] => PDF [FileSize] => 97066 [BatchId] => 427031 [Indices] => stdClass Object ( [RMWSObjectIndex] => Array ( [0] => stdClass Object ( [IndexDataType] => LB [IndexCode] => OBRA [IndexName] => Obra e Fase [IndexValue] => 311C ) [1] => stdClass Object ( [IndexDataType] => LB [IndexCode] => REG [IndexName] => Regional [IndexValue] => Minas Gerais - MG ) [2] => stdClass Object ( [IndexDataType] => TX [IndexCode] => CPFCNPJ [IndexName] => CPF/CNPJ [IndexValue] => 06981180000116 ) [3] => stdClass Object ( [IndexDataType] => TX [IndexCode] => NNF [IndexName] => Nº (Nota Fiscal ou Processo) [IndexValue] => 5502 ) [4] => stdClass Object ( [IndexDataType] => DT [IndexCode] => DATAEMISS [IndexName] => Data de Emissão [IndexValue] => 22/02/2016 ) [5] => stdClass Object ( [IndexDataType] => NU [IndexCode] => VALOR [IndexName] => *Valor [IndexValue] => 58.51 ) [6] => stdClass Object ( [IndexDataType] => LB [IndexCode] => PR [IndexName] => Possui Rateio? [IndexValue] => NÃO ) [7] => stdClass Object ( [IndexDataType] => LB [IndexCode] => TDP [IndexName] => Tipo do Pagamento [IndexValue] => Boleto Anexado (Paga apenas essa nota) ) [8] => stdClass Object ( [IndexDataType] => TX [IndexCode] => NBDB [IndexName] => Nº do Boleto/Dados Bancários [IndexValue] => ) [9] => stdClass Object ( [IndexDataType] => LB [IndexCode] => PP [IndexName] => Pagamento Parcelado? [IndexValue] => NÃO ) [10] => stdClass Object ( [IndexDataType] => DT [IndexCode] => DATVENC [IndexName] => Vencimento (1ª parcela) [IndexValue] => 29/02/2016 ) [11] => stdClass Object ( [IndexDataType] => TX [IndexCode] => DV [IndexName] => Demais Vencimentos [IndexValue] => ) [12] => stdClass Object ( [IndexDataType] => TX [IndexCode] => CB [IndexName] => Código do Boleto [IndexValue] => ) [13] => stdClass Object ( [IndexDataType] => TX [IndexCode] => DESCRICAO [IndexName] => Descrição [IndexValue] => NOVO LANÇAMENTO CEMIG P.5502 ) [14] => stdClass Object ( [IndexDataType] => LB [IndexCode] => DOCSP1 [IndexName] => DOC - SP [IndexValue] => Contas de Consumo ) ) ) ) ) ) )

I need to get various data as for example the CNPJ that is present in this excerpt:

[IndexCode] => CPFCNPJ [IndexName] => CPF/CNPJ [IndexValue] => 06981180000116 

I tried to use foreach() but there are many classes within each other and I can’t move forward.

  • It doesn’t make sense to use a repetition loop to access only one attribute. Why not access it directly by following the object string?

  • Already tried to access the object ? $dados->objeto

1 answer

2

Suppose I received a return in this object format, to access the information of this object, I do not need to transform it into an array, I can access it directly, follows the example below.

Return stdClass

$dados = stdClass Object ( 
            [cliente] => stdClass Object ( 
                            [codigo] => 1 
                            [nome] => Pedro 
                            [documento] => 12345678900 
             ) 
          )

To recover some value of this object of mine just access the attribute that I intend to recover the value.

$dados->cliente->documento

Reference: Objects - PHP

  • It worked! Searching from layer to layer... Simpler than I imagined! Thank you very much! $result->Search_advancedresult->Objects->Rmwsobject2->Objectid;

  • Glad it worked out, I could score as resolved to reach more people?

Browser other questions tagged

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