PHP recursively traverses multi-dimensional arrays to implement unlimited classification

Source: Internet
Author: User
This article mainly introduces PHP recursive traversal of multi-dimensional arrays to achieve unlimited classification. it involves PHP recursive traversal of arrays related techniques and is very useful in linking menus and parent and child columns design, for more information about how to use PHP to recursively traverse multi-dimensional arrays, see the following example. We will share this with you for your reference. The details are as follows:

<? Php // $ data [] = array ('id' => 1, 'parentid' => 0, 'name' => 'China ', 'IMG '=> '000000'); $ data [] = array ('id' => 1, 'parentid' => 0, 'name' => 'China'); $ data [] = array ('id' => 2, 'parentid' => 0, 'name' => 'u.s. '); $ data [] = array ('id' => 3, 'parentid' => 0, 'name' => 'Korea '); $ data [] = array ('id' => 4, 'parentid' => 1, 'name' => 'Beijing'); $ data [] = array ('id' => 5, 'parentid' => 1, 'name' => 'Shanghai'); $ data [] = array ('id' => 6, 'parentid' => 1, 'name' => 'Guangxi '); $ data [] = array ('id' => 7, 'parentid' => 6, 'name' => 'guilin '); $ data [] = array ('id' => 8, 'parentid' => 6, 'name' => 'nanning '); $ data [] = array ('id' => 9, 'parentid' => 6, 'name' => 'Liuzhou '); $ data [] = array ('id' => 10, 'parentid' => 2, 'name' => 'Newyork '); $ data [] = array ('id' => 11, 'parentid' => 2, 'name' => 'Washington '); $ data [] = array ('id' => 12, 'parentid' => 3, 'name' => 'Seoul '); $ tree = build_tree ($ data, 0); // echo memory_get_usage (); print_r ($ tree ); function findChild (& $ arr, $ id) {$ childs = Rray (); foreach ($ arr as $ k => $ v) {if ($ v ['parentid'] = $ id) {$ childs [] = $ v ;}return $ childs;} function build_tree ($ rows, $ root_id) {$ childs = findChild ($ rows, $ root_id ); if (empty ($ childs) {return null;} foreach ($ childs as $ k => $ v) {$ rescurTree = build_tree ($ rows, $ v ['id']); if (null! = $ RescurTree) {$ childs [$ k] ['Childs '] = $ rescurTree;} return $ childs ;}?>

Running result:

Array ([0] => Array ([id] => 1 [parentid] => 0 [name] => China [childs] => Array ([0] => Array ([id] => 4 [parentid] => 1 [name] => Beijing) [1] => Array ([id] => 5 [parentid] => 1 [name] => Shanghai) [2] => Array ([id] => 6 [parentid] => 1 [name] => Guangxi [childs] => Array ([0] => Array ([ id] => 7 [parentid] => 6 [name] => Guilin) [1] => Array ([id] => 8 [parentid] => 6 [name] => Nanning) [2] => Array ([id] => 9 [parentid] => 6 [name] => Liuzhou ))))) [1] => Array ([id] => 2 [parentid] => 0 [name] => US [childs] => Array ([0] => Array ([ id] => 10 [parentid] => 2 [name] => New York) [1] => Array ([id] => 11 [parentid] => 2 [name] => Washington ))) [2] => Array ([id] => 3 [parentid] => 0 [name] => Korea [childs] => Array ([0] => Array ([ id] => 12 [parentid] => 3 [name] => Seoul ))))

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.