PHP array classification, a variety of helpless, rescue

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