Php recursion Infinitus class recursion there are several forms of infinite classification. here I only give examples of the three commonly used: First: returns an array with sorting: & lt ;? Php $ dataarray (1 & gt; array (id & gt; 1, pid & gt; 0, user_name & gt; one,), 2 php recursive Infinitus classification
There are several forms of Recursive Infinite-level classification. here I only give examples of the three commonly used:
First: return the sorted array:
Array ('id' => 1, 'pid '=> 0, 'User _ name' => 'one ',), 2 => array ('id' => 2, 'pid '=> 1, 'User _ name' => 'two ',), 3 => array ('id' => 3, 'pid '=> 1, 'User _ name' => 'two ',), 4 => array ('id' => 4, 'pid '=> 2, 'User _ name' => 'Three ',), 5 => array ('id' => 5, 'pid '=> 2, 'User _ name' => 'Three',),); function genCate ($ data, $ pid = 0, $ level = 0) {$ string = str_repeat ("--", $ level ). '|'; static $ Result = array (); $ result = empty ($ level )? Array (): $ result; foreach ($ data as $ k => $ row) {if ($ row ['pid '] = $ pid) {$ row ['User _ name'] = $ string. $ row ['User _ name']; $ result [] = $ row ['User _ name']; genCate ($ data, $ row ['id'], $ level + 1) ;}}return $ result;} echo'';$result = genCate( $data );print_r( $result );exit;?>
Type 2: return the html string format:
Third: return multi-dimensional array format: