dongxin1999 2017-01-14 04:31
浏览 412

非法字符串偏移量(PHP警告)

I'm using Codeigniter Framework.Actually, I'm trying to display an array in the view.But it shows some error.I've checked my code but still unable to find the mistake. Any help will be highly appreciated.

models/users_model.php

class Users_model extends CI_Model{

            public function getUsers(){
                return array(
                        'first' => 'Farhan Khan',
                        'second' => 'Abdul Wali',
                        'third' => 'Shahzaib Kamal'
                        );
            }

        }

controller/users.php

 class Users extends CI_Controller{

        public function index(){
            $this->load->model('Users_model');
            $data['users'] = $this->Users_model->getUsers();

            $this->load->view('users_list', $data);
        }

    }

views/users_list.php

    <!DOCTYPE html>
<html>
<head>
    <title>Users List</title>
</head>
<body>
    <?php
        foreach ($users as $user) {
            echo $user['first'];
        }
    ?>
</body>
</html>

The error i get is:

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'first'

  • 写回答

4条回答 默认 最新

  • dsrnwngq411594 2017-01-14 04:36
    关注

    Try like this..

    <!DOCTYPE html>
    <html>
    <head>
        <title>Users List</title>
    </head>
    <body>
        <?php
            foreach ($users as $user) {
                echo $user."<br/>";
            }
        ?>
    </body>
    </html>
    

    OR

    <!DOCTYPE html>
        <html>
        <head>
            <title>Users List</title>
        </head>
        <body>
            <?php
                foreach ($users as $key=>$value) {
                     echo $key."=>".$value."<br/>";
                }
            ?>
        </body>
        </html>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题