title: dynamic variables from array
body: <p>I store the field names within an array, in hopes to dynamically create the variables.</p>
  
  <p>I receive a illegal offset type error for the if and else, these two lines:</p>
  
  <pre><code>$data[$tmp_field] = $tmp_field[$id];
  
  $data[$tmp_field] = 0;
  </code></pre>
  
  <p>I checked the post data and it is posting with the appropriate data, but I am not sure what the problem is.</p>
  
  <p>$student_id stores all the students ids., for example: <code>$student_id = array(8,9,11,23,30,42,55);</code></p>
  
  <pre><code>function updateStudentInfo() {
    $student_id = $this-&gt;input-&gt;post('student_id');
    $internet_student = $this-&gt;input-&gt;post('internet_student');
    $dismissed = $this-&gt;input-&gt;post('dismissed');
    $non_matriculated_student = $this-&gt;input-&gt;post('non_matriculated_student');
    $felony = $this-&gt;input-&gt;post('felony');
    $probation = $this-&gt;input-&gt;post('probation');
    $h_number = $this-&gt;input-&gt;post('h_number');
    $office_direct_to = $this-&gt;input-&gt;post('office_direct_to');
    $holds = $this-&gt;input-&gt;post('holds');
  
    $fields = array('internet_student', 'non_matriculated_student', 'h_number', 'felony', 'probation', 'dismissed');
  
    foreach($student_id as $id):
      $data = array();
  
      foreach($fields as $field_name):
        $tmp_field = ${$field_name};
  
        if(empty($tmp_field[$id])) {
          $data[$tmp_field] = 0;
        } else { 
          $data[$tmp_field] = $tmp_field[$id];
        }
      endforeach;
  
      print '&lt;pre style="color:#fff;"&gt;';
      print_r($data);
      print '&lt;/pre&gt;';
  
    endforeach;
  }
  </code></pre>
  
  <p>This is the array format I desire:</p>
  
  <pre><code>Array
  (
      [internet_student] =&gt; 1
      [non_matriculated_student] =&gt; 1
      [h_number] =&gt; 0
      [felony] =&gt; 0
      [probation] =&gt; 1
      [dismissed] =&gt; 0
  )
  </code></pre>
  
  <p>Added screenshot to give you a visual of the form the data is being posted from</p>
  
  <p><img src="https://i.stack.imgur.com/eI4Mk.png" alt="enter image description here"></p>