PHP 数组:根据 id 和parenth id 生成 id_path 和 level

从这个表:
从这个表

我在 PHP 中得到这个数组: PHP 中的数组

我想在这个数组中添加一个新行,我知道category_id和parent_id,如何对id_path和level进行编程?

编辑1:例如最后一行:category_id:107,parent_id:106,如何编码id_path = 96/106/107和级别?id_path:由斜杠分隔的类别 ID 序列

编辑2:

  function test() {

        $arGiven = [ 

            [

                'category_id' => 100,

                'parent_id' => 107,

                'id_path' => '',

                'level' => 0,

            ],

            [

                'category_id' => 106,

                'parent_id' => 96,

                'id_path' => '',

                'level' => 0,

            ],

            [

                'category_id' => 107,

                'parent_id' => 106,

                'id_path' => '',

                'level' => 0,

            ]


        ];


        $arExpected = [ 

            [

                'category_id' => 100,

                'parent_id' => 107,

                'id_path' => '96/106/107/100',

                'level' => 4,

            ],

            [

                'category_id' => 106,

                'parent_id' => 96,

                'id_path' => '96/106',

                'level' => 2,

            ],

            [

                'category_id' => 107,

                'parent_id' => 106,

                'id_path' => '96/106/107',

                'level' => 3,

            ]


        ];


        $result = $arGiven;


        return $result;

    }

但 $newArray 是


  "/107",

  "/107/96",

  "/107/96/106"

]

代替:


[

    '96/106/107/100',

    '96/106',

    '96/106/107'

]


慕哥6287543
浏览 86回答 1
1回答

MYYA

我确信它可以真正优化:<?php&nbsp; &nbsp; $arGiven = [&nbsp;&nbsp; &nbsp; // add the root category&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'category_id' => 96,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'parent_id' => 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id_path' => '',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'level' => 0,&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'category_id' => 100,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'parent_id' => 107,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id_path' => '',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'level' => 0,&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'category_id' => 106,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'parent_id' => 96,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id_path' => '',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'level' => 0,&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'category_id' => 107,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'parent_id' => 106,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id_path' => '',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'level' => 0,&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; ];&nbsp; &nbsp; $level = 0;&nbsp; &nbsp; $parentLevelIds = [0];&nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; $level++;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $nextParentLevelIds = [];&nbsp; &nbsp; &nbsp; &nbsp; $arGiven = array_map(function($value) use($level, $parentLevelIds, &$nextParentLevelIds, $arGiven) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (in_array($value['parent_id'], $parentLevelIds)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $path = '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($value['parent_id'] !== 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $parent = array_filter($arGiven, function($parentSearchValue) use ($value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $parentSearchValue['category_id'] == $value['parent_id'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!empty($parent)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $path = array_shift($parent)['id_path'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $value['level'] = $level;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $value['id_path'] = empty($path) ? $value['category_id'] : ( $path . '/' . $value['category_id'] );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $nextParentLevelIds[] = $value['category_id'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $value;&nbsp; &nbsp; &nbsp; &nbsp; }, $arGiven);&nbsp; &nbsp; &nbsp; &nbsp; $parentLevelIds = $nextParentLevelIds;&nbsp; &nbsp; &nbsp; &nbsp; $hasLevel0 = !empty(array_filter($arGiven, function($value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $value['level'] == 0;&nbsp; &nbsp; &nbsp; &nbsp; }));&nbsp; &nbsp; } while ($hasLevel0);
打开App,查看更多内容
随时随地看视频慕课网APP