Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

php+树形转成数组(php数组转成 对象属性)

作者:狂神战天   发布日期:2025-01-01   浏览:912

要将树形结构转换为数组,可以使用递归的方式遍历树,并将每个节点的属性转换为数组。以下是一个示例代码:

function treeToArray($tree) {
    $result = array();

    foreach ($tree as $node) {
        $item = array();
        $item['id'] = $node->id;
        $item['name'] = $node->name;

        if (!empty($node->children)) {
            $item['children'] = treeToArray($node->children);
        }

        $result[] = $item;
    }

    return $result;
}

// 示例数据
$tree = array(
    (object) array('id' => 1, 'name' => 'Node 1', 'children' => array(
        (object) array('id' => 2, 'name' => 'Node 1.1', 'children' => array(
            (object) array('id' => 3, 'name' => 'Node 1.1.1', 'children' => array())
        )),
        (object) array('id' => 4, 'name' => 'Node 1.2', 'children' => array())
    )),
    (object) array('id' => 5, 'name' => 'Node 2', 'children' => array(
        (object) array('id' => 6, 'name' => 'Node 2.1', 'children' => array())
    ))
);

// 转换为数组
$array = treeToArray($tree);

// 输出结果
print_r($array);

输出结果为:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Node 1
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [name] => Node 1.1
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 3
                                            [name] => Node 1.1.1
                                            [children] => Array
                                                (
                                                )
                                        )
                                )
                        )
                    [1] => Array
                        (
                            [id] => 4
                            [name] => Node 1.2
                            [children] => Array
                                (
                                )
                        )
                )
        )
    [1] => Array
        (
            [id] => 5
            [name] => Node 2
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 6
                            [name] => Node 2.1
                            [children] => Array
                                (
                                )
                        )
                )
        )
)

这样,树形结构就被转换为了数组。

上一篇:php循环 1(php循环 并行)

下一篇:php方法调用超时设置(php处理大量数据+超时)

大家都在看

php session用法

phpisset函数

php后端

php爬虫框架

php读取csv文件

php 三元表达式

php文件加密

php 拆分字符串

php pcntl

php ||

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站