首页 > 解决方案 > 按 ID 删除 JSON [] 数组元素

问题描述

项目框架:CodeIgniter

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

我们在 HTML Table 中列出人员。

<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”表中有一个列,因为“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);

            }

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

标签: phpsqljsonajaxcodeigniter

解决方案


因为您只有元素数组,所以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);
}

推荐阅读