Assigning variables from an array where property doesn't always exist
<p>I'm using an API that returns search results in json.  I decode this to a PHP array and can pick out the elements I need from there by looping over the array and assigning the elements as variables so that I can store the values in a database.</p>

<p>My issue is that there can be one or several dozen properties returned, it just depends on the search parameters.  If I do this, </p>

<pre><code>$array = json_decode(file_get_contents("http://search-query-url&amp;output=json"));

foreach($array as $cur) {
  $key1 = $cur-&gt;item_title;
  $key2 = $cur-&gt;item_url;
  $key3 = $cur-&gt;item_location;
  echo $key1 . " " . $key2 . " " . $key3 "&lt;br&gt;";
  }
</code></pre>

<p>...all is well unless item_location (key3) doesn't exist in the json output.  In such cases, I get the error "Undefined property: stdClass::$item_location."</p>

<p>The property name (item_title, item_url, etc) is important because that is how I know what table column the value/string should be stored in. </p>

<p>I'm stumped trying to figure out this problem, and I'm not quite sure where to go.  Trying to use in_array or in_object but I must be doing something wrong.  I need to create variables based on the corresponding property name, when it exists.</p>

<p>Any thoughts?  Thank you!</p>
