PHP handles parent-child hierarchy selection issues

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

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.