: This article describes how to deal with the parent-child hierarchy selection in PHP. For more information about PHP tutorials, see.
/*** @ Param $ data operation array * @ param string $ fieldPri unique key name, if it is a table, it is the table's primary key * @ param string $ fieldPid parent ID key name * @ param int $ pid level-1 PID value * @ param string $ sid Subid is used to obtain the specified all parent IDs of ID * @ param int $ type operation method 1 => return multi-dimensional array, 2 => returns a one-dimensional array, 3 => get all the parent * @ param string $ html name prefixes of the specified stator ID (parameter $ sid, the list used to display the sense of parallelism in the View * @ param int $ level does not need to pass parameters (called during execution) * @ return array */private function channel ($ data, $ fieldPri = 'CID', $ fieldPid = 'pid', $ pid = 0, $ sid = null, $ type = 2, $ html = "", $ level = 1) {if (! $ Data) {return array ();} switch ($ type) {case 1: $ arr = array (); foreach ($ data as $ v) {if ($ v [$ fieldPid] ==$ pid) {$ arr [$ v [$ fieldPri] = $ v; $ arr [$ v [$ fieldPri] ['html'] = str_repeat ($ html, $ level-1 ); $ arr [$ v [$ fieldPri] ["Data"] = self: channel ($ data, $ fieldPri, $ fieldPid, $ v [$ fieldPri], $ sid, $ type, $ html, $ level + 1) ;}return $ arr; case 2: $ arr = array (); $ id = 0; foreach ($ data as $ v) {if ($ v [$ fieldPid] ==$ pid) {$ arr [$ id] = $ v; $ arr [$ id] ['level'] = $ level; $ arr [$ id] ['html'] = str_repeat ($ html, $ level-1 ); $ sArr = self: channel ($ data, $ fieldPri, $ fieldPid, $ v [$ fieldPri], $ sid, $ type, $ html, $ level + 1 ); $ arr = array_merge ($ arr, $ sArr); $ id = count ($ arr) ;}return $ arr; case 3: static $ arr = array (); foreach ($ data as $ v) {if ($ v [$ fieldPri] ==$ sid) {$ arr [] = $ v; $ sArr = self :: channel ($ data, $ fieldPri, $ fieldPid, $ pid, $ v [$ fieldPid], $ type, $ html, $ level + 1 ); $ arr = array_merge ($ arr, $ sArr) ;}}return $ arr ;}}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
The above describes how to deal with the parent-child hierarchy selection of PHP, including the content, and hope to be helpful to friends who are interested in PHP tutorials.