How can I traverse a multi level php object and unset the deepest node dynamically -


i have object in form:

commentid: [   "userid": userid,    "status": "active/deleted",    "datetime": "datetime",    "comment": "commenttext",     "likes": [     userid: datetime of like,      userid: datetime of like...   ],    "comments": [same parent] ] 

this means, have multiple levels of comments -> comment on comment on comment.

if want delete comment, view full chain of commentids... parent commentid, sub-parent, sub-sub.. etc..

how can unset particular comment.

something this:

imagine following: 1;2;3 1 commentid of parent, 2 commentid of child of parent , 3 commentid of child of child.

this comes me variable in php.

i unset 3..

ideally, i'd write,

unset($object->{1}->{2}->{3}); , work.

but, can't assume number of levels. have somehow loop through comment id's , figure out way.

without example of have, i'm assuming have array of comments, , inside of each comment array of more comments inside comments field.

if case doing along lines of

unset($object->comments[1]->comments[2]->comments[3]);

this unset third comment, of second comment of first comment in object.

if not answer looking for, please give example.

sudo recursion example

function unsetlowestcomment($comment) {   $commentslength = count($comment->comments);   if($commentslength > 0) {     unsetlowestcomment($comment->comments[$commentslength -1]);   } else {     unset($comment);   } }  unsetlowestcomment($commentinquestion); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -