PHP array rating, helpless, rescue
Array (
Array ("Level" = ' 1 ', "caption" = "1"),
Array ("leve1" = ' 1 ', "caption" = "2"),
Array ("leve1" = ' 2 ', "caption" and "Two-level menu 1"),
Array ("leve1" = ' 2 ', "caption" and "Two-level menu 2"),
Array ("leve1" = ' 1 ', "caption" = "3"),
)
where level = 2 is the sub-menu above its nearest leve1 = 1, does not exclude the level = 3 of the Sub-menu (From level = 2) now want to make the following form, think for a long time there is no good way, colleagues suggest I use recursion, can recursive brother don't understand, PHP first contact, It's Danale trouble for everyone.
Array (
Array ("Level" = ' 1 ', "caption" = "1"),
Array ("leve1" = ' 1 ', "caption" = "2",
"Sub" =>array ("leve1" = ' 2 ', "caption" and "Two-level menu 1"),
"Sub" =>array ("leve1" = ' 2 ', "caption" = "Two level menu 2")
),
Array ("leve1" = ' 1 ', "caption" = "3"),
)
php menu? recursion
------Solution--------------------
You can't even search, how to find information to do it ah?
Search "PHP Unlimited Classification"
http://bbs.csdn.net/topics/360028778
http://bbs.csdn.net/topics/250025103
http://bbs.csdn.net/topics/320022124
......
Inside of your array
Level equivalent to PID (change =1 to =0)
Caption split into ID and name
According to the above copy, finished first, have time to understand the recursion method of others
I'm busy doing other things.
------Solution--------------------
I wrote it under the reference.
!--?
$arr = Array (
Array (' ID ' =>1, ' city_name ' = ' China ', ' rel_id ') = ' 1 ', ' pid ' =>0),
Array (' ID ' =>2, ' city_name ' = ' Guangdong ', ' rel_id ' = ' 1-2 ', ' pid ' =>1),
Array (' ID ') =>3, ' city_name ' and ' Shenzhen ', ' rel_id ' and ' =>4 ', ' pid ' =>2,
Array (' ID ' city_name, ' rel_ ' + id ' = ' 1-2-4 ', ' pid ' =>2)
);
Function Find_subclass ($pid) {
Global $arr;
$__arr = Array ();
foreach ($arr as $k = + $v)
{
if ($v [' pid ']== $pid) $__arr[] = $v;
}
return $__arr;
}
Function Tree_subclass ($pid =0) {
$__arr = Array ();
$__arr = Find_subclass ($pid);
if (!empty ($__arr)) {
foreach ($__arr as $k = = $v)
{