Home > database >  Deleting JSON [ ] Array Element By Id
Deleting JSON [ ] Array Element By Id

Time:11-15

Project Framework: CodeIgniter

In project, we used 2 tables such as "person" and "emailGroups". We saved people in the "person" table with json_encode by group ID. Because a person can belong to more than one group.

We list people in 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 table:

person table

We have "person" table has a column as "personEmailGroup" includes JSON Contains such as ["1","2","4"]. We want to delete an Id included JSON in personEmailGroup column. For Example we want to delete only "4", before contains Id's ["1","2","4"], after delete shown as ["1","2"] then update.

Delete Function Code:

$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);

            }

This code is unwork. Maybe it gives any idea about what we want? We need get new ideas about this process with working code. Thanks in advance!

CodePudding user response:

Because you have only array of elements, unset by value is not working. You should use unset by element index - . So you need to find the element index and then call the unset function. Here is your code updated.

$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);
}
  • Related