英文:
Remove object one object and rename all object name sequence wise
问题
object
这是我检索到的对象。如何移除 module_1 对象并重命名 module 对象,例如移除 module_1 并将 module_2、module_3... 重命名为 module_1、module_2...。
{
"module_1": {
"modulename": "maths",
"moduletypes": "mandatory",
"moduleid": "88",
"moduleaim": "MODULE AIM",
"learningobjectives": "LEARNING OBJECTIVES",
"assessmentcriteria": "ASSESSMENT 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
这是我的 HTML 代码。
<button
class="btn rounded-pill btn-primary remove_module remove_module_button"
data-id="5"
data-modulename="module_1"
type="button"
>
移除模块 <i class="bx bx-x-circle ml-1"></i>
</button>
jquery
这是我的 jQuery 代码。
$(document).on('click', '.remove_module', function() {
var id = $(this).data('id');
var modulename = $(this).data('modulename');
var ok = confirm("确定要删除此模块吗?");
if (ok) {
$.ajax({
method: "GET",
url: "deletemodule",
data: {
id: id,
modulename: modulename
},
success: function(data) {
if (data == 1) {
alert("课程删除成功!");
window.location.reload();
}
}
});
}
});
php
这是我的 PHP 代码。
public function deleteModule(Request $request){
$id = $request->id;
$modulename = $request->modulename;
$data = Course::where('id', $id)->get();
$modueldata = $data[0]->modules; //获取 modules 列中的模块数据
}
我需要删除用户提供的对象并按顺序重命名所有模块。
这是我需要执行的操作。
{
"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"
]
}
}
英文:
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
class="btn rounded-pill btn-primary remove_module remove_module_button"
data-id="5"
data-modulename="module_1"
type="button"
>
Remove Module <i class="bx bx-x-circle ml-1"></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.
答案1
得分: 0
这只涉及数组处理和取消设置。
$modules = $data[0]->modules; // 如果它不是数组,也要进行转换
$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++;
}
英文:
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++;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论