【问题标题】:array to string conversion error in codeginiter in controller控制器中codeigniter中的数组到字符串转换错误
【发布时间】:2016-09-26 14:51:30
【问题描述】:
$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,但它显示错误严重性:通知

消息:数组到字符串的转换

【问题讨论】:

标签: php codeigniter-2


【解决方案1】:

方法一

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

方法二

var_dump($data);

方法3

print_r($data);

【讨论】:

  • lang_name 包含多选,除了 lang_name 之外的所有值都在打印。我怎样才能打印这个变量呢? 泰卢固语
    印地语
    英语
  • 你用的是什么方法?
【解决方案2】:

1) 你试图回显 array 变量 ($data)

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

解决方案:

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

【讨论】:

  • lang_name 包含多选,除了 lang_name 之外的所有值都在打印。我怎样才能打印这个变量呢? 泰卢固语
    印地语
    英语
【解决方案3】:
$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;

【讨论】:

  • 泰卢固语
    印地语
    英语
    在上面的数组中,lan​​g_name 包含多选,在 foreach 循环中它没有得到打印跨度>
  • @Danush 您可以更改字段名称。
  • lang_name 包含多选,除了 lang_name 之外的所有值都在打印。我怎样才能打印这个变量呢? 泰卢固语
    印地语
    英语
  • 像这样获取它们:substr(implode(', ', $this->input->post('lang')), 0)
  • 你也可以这样使用 $this->input->post('lang');它将是一个数组,与 $_POST['lang'] 相同。示例 foreach ($this->input->post('lang') as $key => $value) { echo "Index {$key}'s value is {$value}.";不幸的是,如果你需要访问一个特定的索引,你必须先将它转换成一个变量或者使用 $_POST 而不是 $this->input->post()。示例:$assign = $this->input->post('lang');回声$分配[0]; // 第一个值 echo $_POST['lang'][0];
猜你喜欢
  • 1970-01-01
  • 2015-12-18
  • 1970-01-01
  • 2012-12-02
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多