消息:implode():codeigniter 中传递的参数无效

问题描述 投票:0回答:3

代码:

$item_des = implode(',',$this->input->post('item_des'));
$item_cost = implode(',',$this->input->post('item_cost'));
$service_cost = implode(',',$this->input->post('service_cost'));
$total_cost = implode(',',$this->input->post('total_cost'));

$data = array(
            'item_des' => $item_des,
            'item_cost' => $item_cost,
            'service_tax' => $service_cost,
            'total_cost' => $total_cost,
            );

            print_r($data);
$query = $this->db->insert('table_name',$data);

在这段代码中,我有一个表单,我使用 jquery/ajax 来插入表单值,但是当我使用 implode 函数时,它会引发错误消息,即

Message: implode(): Invalid arguments passed

那么,我该如何解决这个问题?请帮助我。

谢谢你

php jquery codeigniter
3个回答
3
投票

您可以将 $a 转换为数组,以确保在使用 implode 时始终使用数组

implode(',', (array) $a);

哪里,

$a=$this->input->post('item_des');

其他情况依此类推。只要尝试一下它肯定会运行!


0
投票

尝试打印每个变量

var_dump($this->input->post('name')); //all these should be array

这些字段很可能不是数组。你有隐藏字段吗?或者你的 html 应该是这样的:

<input name="item_des[]" /> <!-- name should be an array instead of a string name, as shown here -->
<input name="item_des[]" /> <!-- there could be multiple inputs for each of the array you want to `implode` -->

现在 HTML 会在 post 中返回一个数组。请 Implode() 需要一个数组将其转换为字符串。


0
投票

此错误意味着您的变量未定义或不是数组。 在使用您的数据之前,请确保它们按照您的想法进行定义。

您可以使用 isset() 来确保变量已定义,并使用 is_array() 来确保它们是数组。一旦你知道你的变量是什么,你就可以按照它们应该的方式对待它们(也许发送你期望一个正确定义的数组的错误)。

将它们转换为数组不会对您有帮助,因为这意味着数据不是您所期望的。您的其余代码可能会有进一步的问题。

© www.soinside.com 2019 - 2024. All rights reserved.