This article mainly introduces the php recursive method to implement the infinite classification instance code. For more information, see the following array:
The Code is as follows:
$ Items = array (
Array ('id' => 1, 'pid '=> 0, 'name' => 'level 1 11 '),
Array ('id' => 11, 'pid '=> 0, 'name' => 'www .php.net level 12 '),
Array ('id' => 2, 'pid '=> 1, 'name' => 'level 2 21 '),
Array ('id' => 10, 'pid '=> 11, 'name' => 'level 2 22 '),
Array ('id' => 3, 'pid '=> 1, 'name' => 'second 23 '),
Array ('id' => 12, 'pid '=> 11, 'name' => 'second-level 24 '),
Array ('id' => 13, 'pid '=> 12, 'name' => 'third-level 31 '),
Array ('id' => 9, 'pid '=> 1, 'name' => 'second level 25 '),
);
Function:
The Code is as follows:
Function formatTree ($ array, $ pid = 0 ){
$ Arr = array ();
$ Tem = array ();
Foreach ($ array as $ v ){
If ($ v ['pid '] ==$ pid ){
$ Tem = formatTree ($ array, $ v ['id']);
// Determine whether a subarray exists
$ Tem & $ v ['son'] = $ tem;
$ Arr [] = $ v;
}
}
Return $ arr;
}