title: Codeigniter pass array to view, or convert array to object with foreach loop
body: <p>recently I came across a problem where I would need to generate an object from CodeIgniter PHP foreach loop. Basically I need to filter through my email list and generate the object based on the result, then pass it to the view and use another foreach loop to list the content out there.</p>
  
  <p>Here's what I have and it successfully generates an array $email_raw, but I couldn't find the right way to generate it as an object instead:</p>
  
  <p>PHP:</p>
  
  <pre><code>    $email_preliminary=$this-&gt;db-&gt;select('email')-&gt;get('user'); 
  
      $email_raw = array();
  
      foreach ($email_preliminary-&gt;result() as $row):
  
          $email_to_test=$row-&gt;email;
  
          if(filter_var($email_to_test, FILTER_VALIDATE_EMAIL)||preg_match('/\d*[1-9]\d*/', $email_to_test))
          {
              $email_raw[] = $email_to_test;
          }
  
          else{
  
          }
      endforeach;
  </code></pre>
  
  <p>People suggested that I use:</p>
  
  <pre><code>                $email_raw[] = (object)array('email'=&gt;$email_to_test);
  </code></pre>
  
  <p>But the error msg says its not an object. </p>
  
  <p>What are other ways I can generate $email_raw as an object so I can pass it to the view and use foreach to list the content out there?</p>
  
  <p>Many thanks!</p>
  
  <p>Update:</p>
  
  <p>per request, here's rest of the controller:</p>
  
  <pre><code>        $data['record']=array_unique($email_raw);
  
          $this-&gt;load-&gt;view('login&amp;signup/signup_test_get_wrong_email_view', $data);
  </code></pre>
  
  <p>and the view I use:</p>
  
  <pre><code>      &lt;?php foreach ($record-&gt;result() as $row): ?&gt; 
  
      &lt;span&gt;&lt;?php echo $row-&gt;email; ?&gt;&lt;/span&gt;&lt;br&gt;
  
        &lt;?php endforeach; ?&gt;
  </code></pre>