Home > other >  Remove object one object and rename all object name sequence wise
Remove object one object and rename all object name sequence wise

Time:01-09

object

This my retrieved object. how can i remove module_1 object and rename module object ex. remove module_1 and rename module_2, module_3... to module_1 module_2....

`{
  "module_1": {
    "modulename": "maths",
    "moduletypes": "mandatory",
    "moduleid": "88",
    "moduleaim": "MODULE AIM",
    "learningobjectives": "LEARNING OBJECTIVES",
    "assessmentcriteria": "SSESSMENT CRITERIA",
    "scenario": "sfgsfgsdfg",
    "tasknumber": 2,
    "taskarea": [
      "<p>taskarea1sdfgsdfsfgagagasdasdf</p>",
      "<p>asdasdasd</p>"
    ],
    "librarylinks": [
      "asdasdsa",
      "asdasd"
    ],
    "studylinks": [
      "asdasd",
      "asdasd"
    ],
    "videoslinks": [
      "asdasdfsdfsdfsdfsdfsdfsdf",
      "asdasdfsfsdfsf"
    ]
  },
  "module_2": {
    "modulename": "asasdasd",
    "moduletypes": "optional",
    "moduleid": "23443",
    "moduleaim": "asdasgdfg",
    "learningobjectives": "dgdg",
    "assessmentcriteria": "dfgdg",
    "scenario": "dgdgdg",
    "tasknumber": 1,
    "taskarea": [
      "dgdfgdfg"
    ],
    "librarylinks": [
      "dfgdfgdfg"
    ],
    "studylinks": [
      "dgdfgdfg"
    ],
    "videoslinks": [
      "dgdfgdfg",
      "sdfasdfasf"
    ]
  },
  "module_3": {
    "modulename": "science",
    "moduletypes": "optional",
    "moduleid": "3234",
    "moduleaim": "sadfsdfgsdaf",
    "learningobjectives": "asfdsadf",
    "assessmentcriteria": "asdfadsf",
    "scenario": "asdfasdfasf",
    "tasknumber": 1,
    "taskarea": [
      "adfafads"
    ],
    "librarylinks": [
      "afdasfasdf"
    ],
    "studylinks": [
      "asdfasafs"
    ],
    "videoslinks": [
      "afdaafd",
      "asfdasfasf"
    ]
  }
}
`

html

Here is my html code

`<button
  
  data-id="5"
  data-modulename="module_1"
  type="button"
>
  Remove Module <i ></i>
</button>`

jquery

here is my jquery code

`$(document).on('click', '.remove_module', function() {
    var id = $(this).data('id');
    var modulename = $(this).data('modulename');
    var ok = confirm("Are you sure, you want to delete this Module?");
    if (ok) {
        $.ajax({
            method: "GET",
            url: "deletemodule",
            data: {
                id: id,
                modulename: modulename
            },
            success: function(data) {
                if (data == 1) {
                    alert("Course Deleted Successfully!");
                    window.location.reload();
                }
            }
        });
    }
});`

php

Here is my php code

`public function deleteModule(Request $request){
        $id =  $request->id;
        $modulename =  $request->modulename;
        $data = Course::where('id', $id)->get();
        $modueldata = $data[0]->modules; //get module data in modules column

     }`

I need to remove object given by user and rename all module sequence wise.

`{
    "module_1": {  
        "modulename": "asasdasd",
        "moduletypes": "optional",
        "moduleid": "23443",
        "moduleaim": "asdasgdfg",
        "learningobjectives": "dgdg",
        "assessmentcriteria": "dfgdg",
        "scenario": "dgdgdg",
        "tasknumber": 1,
        "taskarea": [
            "dgdfgdfg"
        ],
        "librarylinks": [
            "dfgdfgdfg"
        ],
        "studylinks": [
            "dgdfgdfg"
        ],
        "videoslinks": [
            "dgdfgdfg",
            "sdfasdfasf"
        ]
    },
    "module_2": { 
        "modulename": "science",
        "moduletypes": "optional",
        "moduleid": "3234",
        "moduleaim": "sadfsdfgsdaf",
        "learningobjectives": "asfdsadf",
        "assessmentcriteria": "asdfadsf",
        "scenario": "asdfasdfasf",
        "tasknumber": 1,
        "taskarea": [
            "adfafads"
        ],
        "librarylinks": [
            "afdasfasdf"
        ],
        "studylinks": [
            "asdfasafs"
        ],
        "videoslinks": [
            "afdaafd",
            "asfdasfasf"
        ]
    }
}`

this how i need to do.

CodePudding user response:

It is just array handling and unset.

$modules        = $data[0]->modules; //this is array if not convert it too
$moduleToRemove = $request->modulename;
$filteredArray  = array_filter($modules, fn($module) => !($module === $moduleToRemove), ARRAY_FILTER_USE_KEY);
$finalModule    = [];
$i = 1;
foreach ($filteredArray as $moduleData) {
    $finalModule['module_'.$i] = $moduleData;
    $i  ;
}
  • Related