Recursively obtain the descendant of a two-dimensional array and delete the descendant. # It is easy to use when there is little data # I don't know how to write non-recursive code, here, the non-recursive writing method is used ** to obtain the descendant * @ paramarray $ two-dimensional array to be operated by arr * @ paramint $ id the id to be obtained * @ paramstring $ idkey unique id field *@ paramstring $ pid
Recursively obtain the descendant of a two-dimensional array and delete the descendant. # It is easy to use when there is little data # I don't know how to write non-recursive code, here, describe non-recursive writing/*** obtain the descendant * @ param array $ the two-dimensional array to be operated by arr * @ param int $ id the id to be obtained * @ param string $ idkey is unique id field * @ param string $ pid
Recursively obtain the descendant of a two-dimensional array and delete the descendant.
# Easy to use when there is little data
# I don't know how to write non-recursive code. Here I want to write non-recursive code
/*** Get descendant * @ param array $ the two-dimensional array to be operated by arr * @ param int $ id the number to be obtained * @ param string $ unique idkey id field * @ param string $ pidkey parent number field * @ param int $ whether self contains itself * @ return array */function getChilds ($ arr, $ id, $ idkey, $ pidkey, $ self = 0) {$ c = array (); foreach ($ arr as $ k => $ value) {if ($ self & $ value [$ idkey] =$ id) {$ c [] = $ value; $ self = 0 ;} if ($ value [$ pidkey] ==$ id) {$ c [] = $ value; $ c = array_merge ($ c, getChilds ($ Rr, $ value [$ idkey], $ idkey, $ pidkey, 0) ;}} return $ c ;} /*** remove descendant * @ param array $ two-dimensional array to be operated by arr * @ param int $ id the number to be removed * @ param string $ unique idkey id field * @ param string $ pidkey parent number field * @ param int $ self whether to remove itself * @ param int $ array_values whether to re-index the array * @ return array */function removeChilds ($ arr, $ id, $ idkey, $ pidkey, $ self = 0, $ array_values = 0) {$ c = getChilds ($ arr, $ id, $ idkey, $ pidkey, $ self); foreach ($ c as $ o) {f Oreach ($ arr as $ k =>$ v) {if ($ o [$ idkey] ==$ v [$ idkey]) {unset ($ arr [$ k]); break ;}}return $ array_values? Array_values ($ arr): $ arr ;}