0

Disclaimer: I'm somewhat new to PHP. I'm trying to remove single objects from a json array, but when I attempt to delete the DOM object rectangle (each of which represents an array object), then process.php ends up making a copy of the array and appending it to the original array.

When I click the delete button (class rectangle-delete), I change the deleteStatus hidden input value to delete which I'm trying to pick up in process.php. This is the particular bit in process.php that I thought would do the trick:

 foreach($arr_data as $key => $value) {
        if($value->value != "delete") {
            $arr_data[] = $value;
        }
     }

Here is the entire process.php:

<?php

   //$myFile = "data/data.json";
   $filename = $_POST['store-template-name'];
   $myFile = "data/" . $filename;
   $arr_data = array(); // create empty array

  try
  {
       //Get form data
       $formdata = array(
          'ID'=> $_POST['ID'],
          'attributeName'=> $_POST['attributeName'],
          'deleteStatus'=> $_POST['deleteStatus'],
          'attributeType'=> $_POST['attributeType']
       );
       //Get data from existing json file
       $jsondata = file_get_contents($myFile);
       // converts json data into array
       $arr_data = json_decode($jsondata, true);
       $updateKey = null;
       foreach ($arr_data as $k => $v) {
            if ($v['ID'] == $formdata['ID']) {
                $updateKey = $k;
            }
        }
       //  delete object in json
       foreach($arr_data as $key => $value) {
          if($value->value != "delete") {
              $arr_data[] = $value;
          }
       }
        if ($updateKey === null) {
            array_push($arr_data,$formdata);
        } else {
            $arr_data[$updateKey] = $formdata;
        }

       $jsondata = json_encode($arr_data);

       //write json data into data.json file
       if(file_put_contents($myFile, $jsondata)) {
            echo 'Data successfully saved';
        }
       else
            echo "error";
   }
   catch (Exception $e) {
            echo 'Caught exception: ',  $e->getMessage(), "\n";
   }
?>
4
  • can you print an example of what $arr_data looks like (using something like var_dump() or print_r)? Also, you should only post code that is aboslutely necessary to our understanding of your problem, maybe think about removing some of the fluff in your question. Commented Jan 16, 2018 at 20:12
  • ^ It's better to use var_export(); :) Commented Jan 16, 2018 at 20:13
  • Really do you get delete Id or update id? Or both are availiable simultaneously? Commented Jan 16, 2018 at 20:14
  • @WilliamPerron Sure, $arr_data looks like [{"ID":"12","attributeName":"PatientName","valueX":"413","valueY":"175.71875","valueHeight":"28","valueWidth":"207","valueURL":"www.test.com","attributeType":"missing-element", {"deleteStatus":"no_delete"}}, {"ID":"13","attributeName":"PatientName","valueX":"433","valueY":"155.71243","valueHeight":"44","valueWidth":"254","valueURL":"www.test.com","attributeType":"missing-element"}, {"deleteStatus":"delete"}] Commented Jan 16, 2018 at 20:24

1 Answer 1

1

Maybe you are trying to filter your $arr_data array to remove the entities when the value is "delete", Am I right? If yes, you could try this.

foreach($arr_data as $key => $value) {
   if($value->value != "delete") {
     $arr_data[] = $value;
   }else{
      unset($arr_data[$key]);
   }
}

or this

$arr_data = array_filter($arr_data, function($value){
     return $value->value != "delete";
});
Sign up to request clarification or add additional context in comments.

6 Comments

Anderson, when I attempt the first block of code you suggested (in place of my own at the same spot), it doubles the size of the array by creating duplicates of each ID. Do you know why that is?
And yes, you are correct that I'm trying to remove the objects whose value for the key deleteStatus is delete.
In your example you are looking for wrong key $value->value == "delete" in this case, you should be look for $value->deleteStatus == "delete"
I was looking your json and i think the structure it is wrong. Because you are looking for a key "deleteStatus" that not exists in all objects and in some objects the key exists, but inside an another object, because this, you cant found the value when your call $value->deleteStatus == "delete"`. You must define a standard for your json structure for prevent problems like this.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.