search
Homephp教程php手册php递归方法实现无限分类实例

数组代码如下:

<?php
$items = array(
    array(
        &#39;id&#39; => 1,
        &#39;pid&#39; => 0,
        &#39;name&#39; => &#39;一级11&#39;
    ) ,
    array(
        &#39;id&#39; => 11,
        &#39;pid&#39; => 0,
        &#39;name&#39; => &#39;一级12&#39;
    ) ,
    array(
        &#39;id&#39; => 2,
        &#39;pid&#39; => 1,
        &#39;name&#39; => &#39;二级21&#39;
    ) ,
    array(
        &#39;id&#39; => 10,
        &#39;pid&#39; => 11,
        &#39;name&#39; => &#39;二级22&#39;
    ) ,
    array(
        &#39;id&#39; => 3,
        &#39;pid&#39; => 1,
        &#39;name&#39; => &#39;二级23&#39;
    ) ,
    array(
        &#39;id&#39; => 12,
        &#39;pid&#39; => 11,
        &#39;name&#39; => &#39;二级24&#39;
    ) ,
    array(
        &#39;id&#39; => 13,
        &#39;pid&#39; => 12,
        &#39;name&#39; => &#39;三级31&#39;
    ) ,
    array(
        &#39;id&#39; => 9,
        &#39;pid&#39; => 1,
        &#39;name&#39; => &#39;二级25&#39;
    ) ,
);
?>

函数代码如下:

<?php
function formatTree($array, $pid = 0) {
    $arr = array();
    $tem = array();
    foreach ($array as $v) {
        if ($v[&#39;pid&#39;] == $pid) {
            $tem = formatTree($array, $v[&#39;id&#39;]);
            //判断是否存在子数组
            $tem && $v[&#39;son&#39;] = $tem;
            $arr[] = $v;
        }
    }
    return $arr;
}
?>

其中,数组一定要包含id和pid用以指定数组值之间的层级关系.


文章链接:

随便收藏,请保留本文地址!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor