如何使用 Codeigniter 以 json 格式保存

How to save in json format with Codeigniter

我有以下内容:

<form method="POST" action="<?php echo site_url('admin/updateCoursesIn'); ?>">
    <input type="text" value="1" name="values">
    <input type="text" value="2" name="values">
    <input type="text" value="3" name="values">

    <input type="submit" value="Submit">
</form>

codeigniter 中的函数:

public function updateCoursesIn($from = "") {
            $data['value'] = json_encode($this->input->post('values'));
            $this->db->where('key', 'courses');
            $this->db->update('frontend_settings', $data);
        }

我需要通过 codeigniter 以 json 格式保存这些值,如下所示:

["1","2","3"]

我创建的函数没有按我的需要保存。

要创建 values 数组,将 [] 添加到 name 属性:

<input type="text" value="1" name="values[]">
<input type="text" value="2" name="values[]">

使用这样的命名 $this->input->post('values') 将是一个数组,并将正确编码为 json。