dtjbcda841554 2014-07-09 14:08 采纳率: 0%
浏览 60
已采纳

Codeigniter传递数组以查看,或使用foreach循环将数组转换为对象

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; ?>
  • 写回答

1条回答 默认 最新

  • dougang5993 2014-07-10 13:47
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?