【问题标题】:Deleting JSON [ ] Array Element By Id按 ID 删除 JSON [] 数组元素
【发布时间】:2021-12-25 23:41:07
【问题描述】:

项目框架:CodeIgniter

在项目中,我们使用了“person”和“emailGroups”等2个表。我们使用 json_encode 按组 ID 将人员保存在“person”表中。因为一个人可以属于多个群体。

我们在 HTML 表格中列出人员。

<table>    
<thead>
                        <tr>
                            <th>Name Surname</th>
                            <th>E-Mail</th>
                            <th>Process</th>
                        </tr>
                        </thead>
                        <tbody>
    <tr>
                <td>$personName</td>
                <td>$personEmail</td>
                <td><div class=\"custom-control custom-switch switch-lg custom-switch-danger mr-2 mb-1 mt-1\">
                    <input type=\"checkbox\" class=\"custom-control-input toggleState2\" name=\"mailStatus\" data-id='$groupId' data-target=\"$personId\" data-index=\"$baseUrl\" id=\"customSwitch2$personId\" $checked>
                           <label class=\"custom-control-label\" for=\"customSwitch2$personId\">
                                  <span class=\"switch-text-left\">Remove</span>
                                  <span class=\"switch-text-right\">Removed</span>
                           </label>
                     </div>
                </td>
                </tr>
    </tbody>
    </table>

人员表:

person table

我们在“person”表中有一个列,因为“personEmailGroup”包含 JSON 包含例如 ["1","2","4"]。我们要删除 personEmailGroup 列中包含 JSON 的 Id。例如我们只想删除“4”,之前包含 Id 的 ["1","2","4"],删除后显示为 ["1","2"] 然后更新。

删除功能码:

$processToId = $this->input->post("personId"); // person Id who has multiple e-mail groups.
$processToGroupId = $this->input->post("groupId"); // the group Id contains JSON

$getEmailGroup = $this->db->where("(JSON_CONTAINS(person.personEmailGroup,'[\"$processToGroupId\"]')) > ",0)->where("personId", $processToId)->get('person')->row("personEmailGroup");

$getEmailGroup = json_decode($getEmailGroup);
            foreach ($getEmailGroup as $gets) {
                if (in_array($processToGroupId, $getEmailGroup)) {
                    unset($getEmailGroup[$gets]);
                }
            }
            $data = array(
                "personEmailGroup" => json_encode($getEmailGroup),
            );
            $process = $this->db->where("personId", $processToId)->update("person", $data);

            if ($process) {

                $dataJson['status'] = true;
                echo json_encode($dataJson);

            } else {

                $dataJson['status'] = false;
                echo json_encode($dataJson);

            }

此代码无效。也许它给出了我们想要什么的任何想法?我们需要通过工作代码获得关于这个过程的新想法。 提前致谢!

【问题讨论】:

    标签: php sql json ajax codeigniter


    【解决方案1】:

    因为您只有元素数组,所以 unset 按值不起作用。您应该使用 unset 按元素索引 - 。所以需要找到元素索引,然后调用unset函数。 这是您的代码更新。

    $processToId = $this->input->post("personId"); // person Id who has multiple e-mail groups.
    $processToGroupId = $this->input->post("groupId"); // the group Id contains JSON
    
    $getEmailGroup = $this->db->where("personId", $processToId)->get('person')->row("personEmailGroup");
    
    $getEmailGroup = json_decode($getEmailGroup);
    
    // remove group if exist
    if (($key = array_search($processToGroupId, $getEmailGroup)) !== false) {
        unset($getEmailGroup[$key]);
    }
    
    $data = array(
        "personEmailGroup" => json_encode($getEmailGroup),
    );
    $process = $this->db->where("personId", $processToId)->update("person", $data);
    
    if ($process) {
        $dataJson['status'] = true;
        echo json_encode($dataJson);
    } else {
        $dataJson['status'] = false;
        echo json_encode($dataJson);
    }
    

    【讨论】:

    • 它工作正常。
    猜你喜欢
    • 2018-06-17
    • 2019-05-09
    • 1970-01-01
    • 1970-01-01
    • 2013-02-28
    • 2017-03-10
    • 2016-09-19
    • 2020-10-21
    • 1970-01-01
    相关资源
    最近更新 更多