1

Following is my code to delete an object inside a JSON array.

if($_GET['method']=='deleteStudentReflections'){

        $jsonString = file_get_contents('../admin/data/student_reflection.json');

        $data = json_decode($jsonString, true);
        print_r($data["student_reflections"]); 
        unset($data["student_reflections"][$_GET['obj_val']]);

        $newJsonString = json_encode($data);

        file_put_contents('../admin/data/student_reflection.json', $newJsonString);

        print_r($_GET);

}

it should do this: Before calling function JSON array value:

{"english_courses":[{"name":"Abdullah","detail":"Bad Boy","image_link":"images\/dp.jpg"},{"name":"Course Chin 2","detail":"Course det 2","image_link":"images\/explicit feedly 3.PNG"},{"name":"courses 3 chinese","detail":"courses detail chinese 3","image_link":"images\/site turorial.png"}]}

it should delete any object inside 'english_courses' courses array, but instead of deleting it does this:

{"english_courses":{"0":{"name":"Abdullah","detail":"Bad Boy","image_link":"images\/dp.jpg"},"2":{"name":"courses 3 chinese","detail":"courses detail chinese 3","image_link":"images\/site turorial.png"}}}

this is obj_val code:

function deleteEnglishCourse() { 
           var obj_val=$('#op').val(); 
           console.log(obj_val); 
           var deleteConfirm = confirm("Press 'Ok' to continue, Press 'Cancel' to Cancel "); 
          if (deleteConfirm == true) { 
             $.ajax ({ type: "GET", url: 'delete.php', data: "obj_val="+obj_val+"&method=deleteEnglishCourse", success: function(data) {alert(data); location.reload(); } }); } else { } } 

this is how '#op' is set:

function displayData(data){ 
            var html="<select id='op' class='form-control' onchange='valueSelect(this.value);'>"; for(var i=0;i<data["english_courses"].length;i++){ html+='<option value="'+i+'">' + data["english_courses"][i]["name"] + '</option>'; } 
   html+='</select>'; 
   $('#name_dropdown').append(html); } 

basically dropdown is populated by dynamic data. and when a value is selected it gets to a php file where a particular obj of a json array is deleted based on the value of obj_val

this error only occurs when obj_val is '0' and php function has to delete the first index of the array. Otherwise it works fine

please help !!

  • what is the value of `$_GET['obj_val']`? and is the example you have given from `$data['student_reflections']`? – mauris Jun 27 '16 at 01:26
  • basically dropdown is populated by dynamic data. and when a value is selected it gets to a php file where a particular obj of a json array is deleted based on the value of obj_val – Muhammad Abdullah Ali Jun 27 '16 at 01:42
  • More: this error only occurs when obj_val is '0' and php function has to delete the first index of the array. Otherwise it works fine – Muhammad Abdullah Ali Jun 27 '16 at 01:43
  • 2
    Could you be kind to add these valuable information into the question by editing your question? Thanks! – mauris Jun 27 '16 at 01:50
  • Please [edit] your post to include any additional information you have to your question. Avoid adding this in the comments, as they are harder to read and can be deleted easier. The edit button for your post is just below the post's tags. – Rizier123 Jun 27 '16 at 01:53
  • done sir, kindly help. i'm stuck in middle of this project – Muhammad Abdullah Ali Jun 27 '16 at 01:55

1 Answers1

3

you have to Re-Index $data array, the problem is when you attempt unset($data....) so the first index start from "1" it should be start from "0" , then it will work fine

How to Remove Array Element and Then Re-Index Array?

Community
  • 1
  • 1
Faisal
  • 152
  • 1
  • 1
  • 12