Recursively retrieve and delete the descendant of a two-dimensional array

Source: Internet
Author: User
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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.