最近做分类目录树形图因为插件要求格式弄了个无限级生成树形数据的,现在提交回数据也是原封不动的提交回来,这会我问题来了,又需要转为一维数组于是就有了这个.

首先:

PHP将线性数据转为树形数据(无限级分类目录)

函数:

 


/**
 * 树形数据转换为线性数据(多维数组转为一维数组)
 *
 * @param array $arrData    需要转换的树形数据
 * @param string $strChild  子集数组字段
 * @return void
 * 格式
 * [[0]=>[‘下标’=>’内容’,’子集下标’=>’子集’],[0]=>[‘下标’=>’内容’,’子集下标’=>’子集’]]
 */
function arreach($arrData = [], $strChild = “children”)
{
    if (empty($arrData) || !is_array($arrData)) {
        return $arrData;
    }
    $arrRes = [];
    foreach ($arrData as $k => $v) {
        $arrTmp = $v;
        unset($arrTmp[$strChild]);
        $arrRes[] = $arrTmp;
        if (!empty($v[$strChild])) {
            $arrTmp = arreach($v[$strChild]);
            $arrRes = array_merge($arrRes, $arrTmp);
        }
    }
    return $arrRes;
}

提交:


Array
(
    [0] => Array
        (
            [id] => 0
            [cid] => 2
            [pid] => 28
            [dname] => 测试一层_2
            [sort] => 50
            [addtime] => 1576572236
            [spread] => true
            [title] => B公司B公司
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [cid] => 2
                            [pid] => 0
                            [dname] => 运营部
                            [sort] => 50
                            [addtime] => 1559811865
                            [spread] => true
                            [title] => 运营部
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 3
                                            [cid] => 2
                                            [pid] => 1
                                            [dname] => 运营一部
                                            [sort] => 50
                                            [addtime] => 1559811865
                                            [spread] => true
                                            [title] => 运营一部
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 22
                                                            [cid] => 2
                                                            [pid] => 3
                                                            [dname] => 一部管理
                                                            [sort] => 50
                                                            [addtime] => 1576484241
                                                            [spread] => true
                                                            [title] => 一部管理
                                                            [children] => Array
                                                                (
                                                                    [0] => Array
                                                                        (
                                                                            [id] => 23
                                                                            [cid] => 2
                                                                            [pid] => 22
                                                                            [dname] => 管理部一组
                                                                            [sort] => 50
                                                                            [addtime] => 1576484277
                                                                            [spread] => true
                                                                            [title] => 管理部一组
                                                                            [children] => Array
                                                                                (
                                                                                    [0] => Array
                                                                                        (
                                                                                            [id] => 24
                                                                                            [cid] => 2
                                                                                            [pid] => 23
                                                                                            [dname] => 一组内小队
                                                                                            [sort] => 50
                                                                                            [addtime] => 1576484305
                                                                                            [spread] => true
                                                                                            [title] => 一组内小队
                                                                                            [children] => Array
                                                                                                (
                                                                                                    [0] => Array
                                                                                                        (
                                                                                                            [id] => 26
                                                                                                            [cid] => 2
                                                                                                            [pid] => 24
                                                                                                            [dname] => 小队内小组
                                                                                                            [sort] => 50
                                                                                                            [addtime] => 1576484391
                                                                                                            [spread] => true
                                                                                                            [title] => 小队内小组
                                                                                                            [children] => Array
                                                                                                                (
                                                                                                                    [0] => Array
                                                                                                                        (
                                                                                                                            [id] => 27
                                                                                                                            [cid] => 2
                                                                                                                            [pid] => 26
                                                                                                                            [dname] => 小组内个人
                                                                                                                            [sort] => 50
                                                                                                                            [addtime] => 1576484437
                                                                                                                            [spread] => true
                                                                                                                            [title] => 小组内个人
                                                                                                                        )

                                                                                                                )

                                                                                                        )

                                                                                                )

                                                                                        )

                                                                                )

                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                    [1] => Array
                                        (
                                            [id] => 4
                                            [cid] => 2
                                            [pid] => 1
                                            [dname] => 运营二部
                                            [sort] => 50
                                            [addtime] => 1559811865
                                            [spread] => true
                                            [title] => 运营二部
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 2
                            [cid] => 2
                            [pid] => 0
                            [dname] => 网络技术部
                            [sort] => 50
                            [addtime] => 1559811865
                            [spread] => true
                            [title] => 网络技术部
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 5
                                            [cid] => 2
                                            [pid] => 2
                                            [dname] => 开发部
                                            [sort] => 50
                                            [addtime] => 1559811865
                                            [spread] => true
                                            [title] => 开发部
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 7
                                                            [cid] => 2
                                                            [pid] => 5
                                                            [dname] => 开发部1办
                                                            [sort] => 50
                                                            [addtime] => 1559811865
                                                            [spread] => true
                                                            [title] => 开发部1办
                                                        )

                                                )

                                        )

                                    [1] => Array
                                        (
                                            [id] => 11
                                            [cid] => 2
                                            [pid] => 2
                                            [dname] => 产品部
                                            [sort] => 50
                                            [addtime] => 1559811865
                                            [spread] => true
                                            [title] => 产品部
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 13
                                                            [cid] => 2
                                                            [pid] => 11
                                                            [dname] => 产品123
                                                            [sort] => 50
                                                            [addtime] => 1559812846
                                                            [spread] => true
                                                            [title] => 产品123
                                                        )

                                                )

                                        )

                                )

                        )

                    [2] => Array
                        (
                            [id] => 28
                            [cid] => 2
                            [pid] => 0
                            [dname] => 顶级部门
                            [sort] => 50
                            [addtime] => 1576572175
                            [spread] => true
                            [title] => 顶级部门
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 29
                                            [cid] => 2
                                            [pid] => 28
                                            [dname] => 测试一层
                                            [sort] => 50
                                            [addtime] => 1576572192
                                            [spread] => true
                                            [title] => 测试一层
                                        )

                                    [1] => Array
                                        (
                                            [id] => 30
                                            [cid] => 2
                                            [pid] => 28
                                            [dname] => 测试一层_2
                                            [sort] => 50
                                            [addtime] => 1576572236
                                            [spread] => true
                                            [title] => 测试一层_2
                                        )

                                )

                        )

                )

        )

)

结果:


Array
(
    [0] => Array
        (
            [id] => 0
            [cid] => 2
            [pid] => 28
            [dname] => 测试一层_2
            [sort] => 50
            [addtime] => 1576572236
            [spread] => true
            [title] => B公司B公司
        )

    [1] => Array
        (
            [id] => 1
            [cid] => 2
            [pid] => 0
            [dname] => 运营部
            [sort] => 50
            [addtime] => 1559811865
            [spread] => true
            [title] => 运营部
        )

    [2] => Array
        (
            [id] => 3
            [cid] => 2
            [pid] => 1
            [dname] => 运营一部
            [sort] => 50
            [addtime] => 1559811865
            [spread] => true
            [title] => 运营一部
        )

    [3] => Array
        (
            [id] => 22
            [cid] => 2
            [pid] => 3
            [dname] => 一部管理
            [sort] => 50
            [addtime] => 1576484241
            [spread] => true
            [title] => 一部管理
        )

    [4] => Array
        (
            [id] => 23
            [cid] => 2
            [pid] => 22
            [dname] => 管理部一组
            [sort] => 50
            [addtime] => 1576484277
            [spread] => true
            [title] => 管理部一组
        )

    [5] => Array
        (
            [id] => 24
            [cid] => 2
            [pid] => 23
            [dname] => 一组内小队
            [sort] => 50
            [addtime] => 1576484305
            [spread] => true
            [title] => 一组内小队
        )

    [6] => Array
        (
            [id] => 26
            [cid] => 2
            [pid] => 24
            [dname] => 小队内小组
            [sort] => 50
            [addtime] => 1576484391
            [spread] => true
            [title] => 小队内小组
        )

    [7] => Array
        (
            [id] => 27
            [cid] => 2
            [pid] => 26
            [dname] => 小组内个人
            [sort] => 50
            [addtime] => 1576484437
            [spread] => true
            [title] => 小组内个人
        )

    [8] => Array
        (
            [id] => 4
            [cid] => 2
            [pid] => 1
            [dname] => 运营二部
            [sort] => 50
            [addtime] => 1559811865
            [spread] => true
            [title] => 运营二部
        )

    [9] => Array
        (
            [id] => 2
            [cid] => 2
            [pid] => 0
            [dname] => 网络技术部
            [sort] => 50
            [addtime] => 1559811865
            [spread] => true
            [title] => 网络技术部
        )

    [10] => Array
        (
            [id] => 5
            [cid] => 2
            [pid] => 2
            [dname] => 开发部
            [sort] => 50
            [addtime] => 1559811865
            [spread] => true
            [title] => 开发部
        )

    [11] => Array
        (
            [id] => 7
            [cid] => 2
            [pid] => 5
            [dname] => 开发部1办
            [sort] => 50
            [addtime] => 1559811865
            [spread] => true
            [title] => 开发部1办
        )

    [12] => Array
        (
            [id] => 11
            [cid] => 2
            [pid] => 2
            [dname] => 产品部
            [sort] => 50
            [addtime] => 1559811865
            [spread] => true
            [title] => 产品部
        )

    [13] => Array
        (
            [id] => 13
            [cid] => 2
            [pid] => 11
            [dname] => 产品123
            [sort] => 50
            [addtime] => 1559812846
            [spread] => true
            [title] => 产品123
        )

    [14] => Array
        (
            [id] => 28
            [cid] => 2
            [pid] => 0
            [dname] => 顶级部门
            [sort] => 50
            [addtime] => 1576572175
            [spread] => true
            [title] => 顶级部门
        )

    [15] => Array
        (
            [id] => 29
            [cid] => 2
            [pid] => 28
            [dname] => 测试一层
            [sort] => 50
            [addtime] => 1576572192
            [spread] => true
            [title] => 测试一层
        )

    [16] => Array
        (
            [id] => 30
            [cid] => 2
            [pid] => 28
            [dname] => 测试一层_2
            [sort] => 50
            [addtime] => 1576572236
            [spread] => true
            [title] => 测试一层_2
        )

)
说点什么
支持Markdown语法
好耶,沙发还空着ヾ(≧▽≦*)o
Loading...