PHP recursively deletes a value from a multi-dimensional array. php recursively deletes a multi-dimensional array.
In today's business logic process, you need to delete a specific key from an uncertain multi-dimensional array. After checking the key for a long time and adding your own modifications, the business logic is finally satisfied, this method should be applicable to many places after modification, so it should be recorded for future use. Here I am a multi-dimensional array or after json_encode, the main purpose is to delete all data with tag_id = 264 in the old_tags_id array. By the way, you must delete the corresponding tag_name and reduce the addtag by 1. The Code is as follows:
Let's put recursive functions first. Of course, this is the core. Many people have read this and already know how to use it.
public function dealMenuTagDelete(&$menuData, $tag_id) { if (!is_array($menuData)) { return false; } foreach ($menuData as $key => $val) { if ($key === 'old_tags_id') { foreach ($val as $tagKey => $tagVaule) { if ($tagVaule == $tag_id) { unset($menuData['old_tags_id'][$tagKey]); unset($menuData['old_tags'][$tagKey]); $menuData['addtag']--; } } } if (is_array($val)) { self::dealMenuTagDelete($menuData[$key], $tag_id); } } return $menuData; }
The dimension leader is in the format of "jeson_encode:
[{"Typ": "res_ejcd", "tit": "message1", "data": "1 @ 2 @ 3", "subdata": {"zizicd1 ": {"typ": "res_wb", "data": "qqqq", "addtag": 1, "old_tags": ["\" campaign integrated analysis \"", "test add a tag"], "old_tags_id": ["264", "17"], "new_tags_id": [], "tit": "1 "}, "zizicd2": {"typ": "res_wb", "data": "wwww", "addtag": 1, "old_tags": ["test add a tag"], "old_tags_id": ["17"], "new_tags_id": [], "tit": "2"}, "zizicd3": {"typ": "res_wb ", "data": "ddd", "addtag": 1, "old_tags": [], "old_tags_id": [], "new_tags_id": ["new_tag1"], "tit": "3" }}, "addtag": 0, "old_tags": [], "old_tags_id": [], "new_tags_id": []}, {"typ": "res_tw", "tit": "Post", "data": "2121", "subdata": null, "addtag": 1, "old_tags": ["519 quotes reading user message text"], "old_tags_id": ["183"], "new_tags_id": [] },{ "typ ": "res_wb", "tit": "Today1", "data": "12312321", "subdata": null, "addtag": 1, "old_tags": [], "old_tags_id": [], "dynamicdata": "{\" default \ ": {\" message_type \ ": \" 1 \ ", \" val \": \ "dynamic \" },\ "segementation \": [{\ "segId \": \ "451 \", \ "message_type \": \ "1 \", \ "val \": \ "You belong to the conversation 80 group \" },{ \ "segId \": \ "450 \", \ "message_type \": \ "1 \", \ "val \": \ "poll-1212 \ n three people, rainbow H T_T \"}]} "," configId ": "2392", "new_tags_id": ["new_tag2"]}]
Then assign values and call
$ Tag_id = 264; $ menuData = json_decode ($ data, true); // here, the second parameter with true will be converted to an array self: dealMenuTagDelete ($ menuData, $ tag_id); print_r ($ menuData );
The above section describes how to recursively delete a value in a multi-dimensional array in PHP. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!