Php 编码点火器foreach循环错误

Php 编码点火器foreach循环错误,php,codeigniter,loops,foreach,Php,Codeigniter,Loops,Foreach,我在codeigniter中得到了一个foreach循环错误,它工作得很好,但是一旦我将项目移动到一个新的宿主,我就得到了一个循环错误。两台服务器都使用PHP7.2 以下是第一个错误: Message: array_slice() expects parameter 1 to be array, null given 这是第二个错误: Message: Invalid argument supplied for foreach() 回溯显示错误在控制器中,我检查了很多次,尝试了很多方法,但仍

我在codeigniter中得到了一个
foreach
循环错误,它工作得很好,但是一旦我将项目移动到一个新的宿主,我就得到了一个循环错误。两台服务器都使用PHP7.2

以下是第一个错误:

Message: array_slice() expects parameter 1 to be array, null given
这是第二个错误:

Message: Invalid argument supplied for foreach()
回溯显示错误在控制器中,我检查了很多次,尝试了很多方法,但仍然是一样的

使用
is_array
PHP函数不会有帮助,因为它只会隐藏错误消息,但函数不会像以前那样工作

控制器:

public function login(){
            $data['title'] = 'Sign In';
            $this->form_validation->set_rules('email', 'Email', 'required');
            $this->form_validation->set_rules('password', 'Password', 'required');
            if($this->form_validation->run() === FALSE){
                $this->load->view('templates/header', $data);
                $this->load->view('users/login', $data);
                $this->load->view('templates/footer');
            } else {

                // Get username
                $username = $this->input->post('email');
                // Get and encrypt the password
                $password = $this->input->post('password');
                // Login user
                $user_id = $this->user_model->login($username, $password);
                if($user_id){
                    // Create session
                    $user_data = array(
                        'id' => $user_id,
                        'email' => $username,
                        'logged_in' => true
                    );
                    $this->session->set_userdata($user_data);
                    // Set message
                    $this->session->set_flashdata('user_loggedin', 'Login Success');
                    redirect('users/account');
                } else {
                    // Set message
                    $this->session->set_flashdata('login_failed', 'Login Faild');
                    redirect('users/login');
                }
            }
        }
    public function account($id = NULL){
      if(!$this->session->userdata('logged_in')){
                redirect('users/login');
            }
        $data['users'] = $this->user_model->get_users($this->session->userdata('id'));
        $data['title'] = 'Account';
        $this->load->view('templates/user_header', $data);
        $this->load->view('users/account', $data);
        $this->load->view('templates/user_footer');
    }
循环代码:

<?php foreach (array_slice($users, 0, 1) as $user): ?>
      <div><p><?php echo $user['first_name'] ?></p></div>
   <?php endforeach; ?>

重写以下代码:

<?php foreach (array_slice($users, 0, 1) as $user): ?>
   <div><p><?php echo $user['first_name'] ?></p></div>
<?php endforeach; ?>

此代码将不会返回多维数组。。。您已经使用了
行数组()
,这将是一个单独的数组。

显示您的获取用户方法。您如何判断使用is\u数组PHP函数是否有用,您确定
foreach()
中的
$users
是一个
数组()
吗。而且,
is_array()
不会隐藏错误,它会在执行和生成之前阻止
foreach()
语句error@pradeep我添加了get_用户method@Sinto我确信$users,
是数组
阻止了
foreach()
station,这就是为什么添加它不会有帮助的原因。因为如果阻止了
foreach()。这里不需要foreach,因为查询中只有记录。避免使用foreach&尝试echo$users['first_name'];非常感谢你的解释。它像以前一样工作得很好。
<?php foreach (array_slice($users, 0, 1) as $user): ?>
   <div><p><?php echo $user['first_name'] ?></p></div>
<?php endforeach; ?>
<div><p><?php echo $users['first_name'] ?></p></div>
public function get_users($id){
      $this->db->join('user_details', 'user_details.user_details_id = users.id');
      $this->db->join('user_orders', 'user_orders.user_id = users.id');
      $query = $this->db->get_where('users', array('id' => $id));
      return $query->row_array();
    }