Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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.

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:

PHP:

    $email_preliminary=$this->db->select('email')->get('user'); 

    $email_raw = array();

    foreach ($email_preliminary->result() as $row):

        $email_to_test=$row->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;

People suggested that I use:

                $email_raw[] = (object)array('email'=>$email_to_test);

But the error msg says its not an object.

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?

Many thanks!

Update:

per request, here's rest of the controller:

        $data['record']=array_unique($email_raw);

        $this->load->view('login&signup/signup_test_get_wrong_email_view', $data);

and the view I use:

      <?php foreach ($record->result() as $row): ?> 

    <span><?php echo $row->email; ?></span><br>

      <?php endforeach; ?>
share|improve this question
    
At the end of generating the $email_raw, use $email_raw = (object)$email_raw;. The whole $email_raw variable becomes object. – machineaddict Jul 9 '14 at 14:10
    
Why do you want to generate an object instead of an array? An array will work just fine and it is generally more versatile. – Dan Jul 9 '14 at 14:17
    
I think you've mistaken this for an associative array otherwise there is no need to cast your array to an object since there is no difference between the usage of these two in your case. – MahanGM Jul 9 '14 at 14:21
    
@machineaddict I tried but the output from the view says: Object of class stdClass could not be converted to string. Is there a way I can generate $email_raw directly as object in the foreach loop? – user3820799 Jul 9 '14 at 14:23
    
@Dan and MahanGM, CodeIgniter view says I have to use an object otherwise it says: Call to a member function result() on a non-object in – user3820799 Jul 9 '14 at 14:24
up vote 1 down vote accepted

foreach can iterate object and arrays. You don't have to convert your array to object.

Leave the controller like it is above:

$email_preliminary=$this->db->select('email')->get('user'); 

$email_raw = array();

foreach ($email_preliminary->result() as $row):

    $email_to_test=$row->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

$data['record']=array_unique($email_raw);

$this->load->view('login&signup/signup_test_get_wrong_email_view', $data);

In the view you simply iterate that array you've just constructed:

<?php foreach ($record as $email): ?> 

    <span><?php echo $email; ?></span><br>

<?php endforeach; ?>

I guess your doubt comes from lack of knowing how the $data['record'] variable looks like. Use print_r($data['record']); right after $data['record'] is being set.

share|improve this answer
    
tnx a lot. it really helped me. – Sathya Baman Aug 12 '14 at 9:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.