Accessing object properties by variable
<p>I've seen this asked before, but my particular case appears to be someone odd, and I'm unable to resolve it - any insight would be appreciated.</p>

<p>I'm trying to access an object property with a variable value, ie.</p>

<pre><code>$foo = new Object();
$foo-&gt;first = 'bar';

$array = array(0 =&gt;'first', 1 =&gt;'second');

$var = 0;

return $foo-&gt;{$array[$var]};
</code></pre>

<p>This is throwing an error "Notice: Undefined property: stdClass::$first".  Removing the braces  returns an identical result.</p>

<p>What don't I understand? (Actual code and error below - the error is as recorded in a Drupal Watchdog log.)</p>

<pre><code>private function load_questionnaire_queue($type, $comparator_id, $comparing_id_array)
{
  $queue = array();
  $type_map = array(
    0 =&gt; "field_portfolio_district['und'][0]['nid']",
    1 =&gt; "field_time_period['und'][0]['tid']",
  );

  foreach ($this-&gt;questionnaires as $q)
  {

    // The commented code below works as expected
    // if ($q-&gt;field_portfolio_district['und'][0]['nid'] == $comparator_id &amp;&amp;
    //      in_array($q-&gt;field_time_period['und'][0]['tid'], $comparing_id_array))

    // This returns an identical error, with or without braces:
    if ($q-&gt;{$type_map[$type]} == $comparator_id &amp;&amp; 
            in_array($q-&gt;{$type_map[!$type]}, $comparing_id_array))
    {
      $queue[] = node_view($q, $view_mode = 'full');
    }
  }

  $this-&gt;queue = $queue;
}
</code></pre>

<blockquote>
  <p>Notice: Undefined property:
  stdClass::$field_portfolio_district['und'][0]['nid'] in
  ComparisonChart->load_questionnaire_queue()</p>
</blockquote>
