php - 按 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);
}
此代码无效。也许它给出了我们想要什么的任何想法?我们需要通过工作代码获得关于这个过程的新想法。提前致谢!
解决方案
因为您只有元素数组,所以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);
}
推荐阅读
- substrate - 如何将 Polkadot-js API 与 Substrate 节点模板一起使用?
- python - UpdateView 使用过滤器时不显示现有数据
- azure - 需要 Azure 应用程序网关中的基本身份验证功能
- java - Jackson JsonIgnore 在 kotlin 中不起作用
- c++ - 在 Ubuntu 中使用 VS Code 进行调试
- java - 由于主方法/java类文件异常错误,我的程序将无法运行
- javascript - ReactJS:TypeError:渲染不是函数updateContextConsumer
- python - List 或 Pandas 中的布尔运算——错误或误解?
- django - 带有表单的图像字段未提交图像
- php - 如何从其他表中获得与多个 where 子句的完全匹配?