0

I get some user information from linked api as json objects. I parse the json using json_decode method. It works just fine. My problem is when a field doesn't exist in the json. For example position object sometimes won't have the endDate property.

    $endDate = $user->positions->values[$i]->endDate->year."-".$user->positions->values[$i]->endDate->month."-01"; 

when the endDate property doesn't exist it gives me the undefined propery error. and code fails. I tried to use try catch but it still gives the error in try. im a newbie php coder. How should I detect that a property is undefined before making a call to that property?

Tolga Evcimen
  • 7,112
  • 11
  • 58
  • 91
  • possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – deceze Jun 23 '13 at 18:11

1 Answers1

1

You can verify if a field exists the following way :

if (isset($user->positions->values[$i]->endDate))
{
    $endDate = $user->positions->values[$i]->endDate->year."-".$user->positions->values[$i]->endDate->month."-01";
}
else
{
    $endDate = null; // Or whatever you want it to be
}
Dany Caissy
  • 3,176
  • 15
  • 21