按 ID 删除 JSON [ ] 数组元素

Deleting JSON [ ] Array Element By Id

项目框架:CodeIgniter

在项目中,我们使用了2个table,例如“person”和“emailGroups”。我们通过组 ID 将人保存在 table 和 json_encode 的“人”中。因为一个人可以属于多个群体。

我们列出 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>

人table:

person table

我们有“person”table 有一列为“personEmailGroup”,包括 JSON 包含如 [“1”,“2”,“4”]。我们要删除 personEmailGroup 列中包含的 ID JSON。例如,我们只想删除“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);

            }

此代码无效。也许它让我们知道我们想要什么?我们需要通过工作代码获得有关此过程的新想法。 提前致谢!

因为您只有元素数组,按值 unset 不起作用。您应该使用 unset by element index - 。所以需要找到元素索引,然后调用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);
}