Undefined Property: stdClass

Asked

Viewed 1,607 times

0

I have a question regarding an array. I have the following information: inserir a descrição da imagem aqui

What I’m not getting is to go through the other array. Below is the code.

 foreach ($file_contents->retorno->contatos as $contatos) {
echo "{$contatos->contato->codigo} - {$contatos->contato->nome} - ";
foreach ($contatos->contato->tiposContato as $contato) {
    dd($contato->tipoContato->descricao);
}
echo '<br>';
 }

When I put the dd it works.

inserir a descrição da imagem aqui

When printing with echo he of error.

foreach ($file_contents->retorno->contatos as $contatos) {
echo "{$contatos->contato->codigo} - {$contatos->contato->nome} - ";
foreach ($contatos->contato->tiposContato as $contato) {
    echo $contato->tipoContato->descricao;
}
echo '<br>';

}

inserir a descrição da imagem aqui

  • Add if(!isset($contatos->contato->tiposContato)){ dd($contatos->contato); } in the first line of its second foreach to verify which data is in the error

  • @Erloncharles, I added the if inside the second foreach, but it keeps giving the following error Undefined Property: stdClass::$tipsContact

  • put before the foreach

1 answer

1

First of all, I recommend you use the foreach instead of a for() to iterate an array. (even if you lose a little performance, it makes reading much easier)

There is the possibility of some of the data inside your $file_contents->retorno->contatos not having the property tiposContato, therefore, I recommend using a isset() to print the property only if it exists.

foreach ($file_contents->retorno->contatos as $contatos) {
    echo "{$contatos->contato->codigo} - {$contatos->contato->nome} - ";

    if(isset($contatos->contato->tiposContato)){
        foreach ($contatos->contato->tiposContato as $contato) {
            echo isset($contato->tipoContato) ? $contato->tipoContato->descricao) : false;
        }
    }

    echo '<br>';
}

To verify at what point the inconsistency may be happening you can give a dd() conditional

if(!isset($contatos->contato->tiposContato)){
    dd($contatos->contato);
}
  • Good Afternoon Erlon, .

  • 1

    I think the second foreach would be in $contatos->tipoContato since contact type is a array.

  • Good afternoon Kayo, I’ve done it, but it still doesn’t work.

  • 2

    @shadow, as I said, most likely some of the data does not possess tipoContato, when you use the dd() it stops at the first iteration of the array, which is probably an element that has tipoContato, then it works, but if an element in the middle of the array does not possess, it will stop and generate error at that point.

  • @Erloncharles, I did that validation you suggested, but it always falls into the false. Print nothing

  • @shadow added a check with a dd() so you’ll be able to understand what’s happening so as not to display the data you need

  • @Erloncharles, with this check of the same error, Undefined Property: stdClass::$tipsContact

  • Aaaaaa, so the error is before, I will edit the answer

Show 3 more comments

Browser other questions tagged

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