控制器中codeginiter中的数组到字符串转换错误

时间:2016-05-28 09:42:07

标签: php codeigniter-2

$data = array('first_name' => $this->input->post('fname'),
'middle_name' => $this->input->post('mname'),

'last_name' => $this->input->post('lname'),
'email_name' => $this->input->post('email'),
'pwd_name' => $this->input->post('pwd'),

    'cno_name' => $this->input->post('cno'),
    'gender_name' => $this->input->post('gender'),
    'country_name' => $this->input->post('country'),
    'lang_name' => $this->input->post('lang')

);
            echo $data;

我想回显或打印$ data,但它显示错误Severity:Notice

消息:数组到字符串转换

3 个答案:

答案 0 :(得分:3)

方法1

foreach($data as $key => $val)
    echo($key.' => '.(is_array($val)?implode(',', $val):$val).'<br>');

方法2

var_dump($data);

方法3

print_r($data);

答案 1 :(得分:0)

1)您尝试回显数组变量($ data)

2)您无法将 echo 用于数组变量

<强>溶液

3)您可以使用 var_dump($ data) print_r($ data)

答案 2 :(得分:0)

$data = array('first_name' => $this->input->post('fname'),
'middle_name' => $this->input->post('mname'),

'last_name' => $this->input->post('lname'),
'email_name' => $this->input->post('email'),
'pwd_name' => $this->input->post('pwd'),

    'cno_name' => $this->input->post('cno'),
    'gender_name' => $this->input->post('gender'),
    'country_name' => $this->input->post('country'),
    'lang_name' => $this->input->post('lang')

);

现在php中有foreach循环。你必须遍历数组。

    foreach($data  as $key => $value)
    {
      echo $key." has the value". $value;
    }

如果您只想在值之间添加逗号,请考虑使用implode

    $string=implode(",",$data);
    echo $string;