亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP 樹依賴列表僅適用于某些子項

PHP 樹依賴列表僅適用于某些子項

PHP
蠱毒傳說 2023-11-03 15:47:15
我正在尋找一個可以創建分層數組但僅適用于某些孩子的函數。這個例子似乎不錯: Recursive function togenerate multiDimensional array from database result但我想要父母只需要一些身份證。例如 :+-------+---------------+---------------------------+|   id  |   parent_id   |           title           |+-------+---------------+---------------------------+|   1   |       0       |   Parent Page             ||   2   |       1       |   Sub Page                ||   3   |       2       |   Sub Sub Page            ||   4   |       0       |   Another Parent Page     ||   5   |       1       |   Sub Page 2              |+-------+---------------+---------------------------+我只想要 id 2、4 和 5 的層次結構。并返回給我這樣的東西:Array(    [0] => Array        (            [id] => 1            [parent_id] => 0            [title] => Parent Page            [children] => Array                        (                            [0] => Array                                (                                    [id] => 2                                    [parent_id] => 1                                    [title] => Sub Page                                                                                    ),                            [1] => Array                                (                                    [id] => 5                                    [parent_id] => 1                                    [title] => Sub Page 2                                                                                     )                        )        )    [1] => Array        (            [id] => 4            [parent_id] => 0            [title] => Another Parent Page        ))2、4 和 5 應該是最小的孩子,并且沒有低于他們的孩子。繼續,我想要的與鏈接的帖子完全相同,但我最小的葉子應該只是數組中存在的葉子的 id [2,4,5]不知道有沒有人明白我的問題...
查看完整描述

1 回答

?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

是的,你的問題很清楚。您想要從下到上重新創建樹,僅針對特定葉子的 id。


您可以通過從初始數組中過濾掉所有不必要的類別來實現它,然后使用您之前找到的函數構建樹。


//Assuming you have this array with mysql result of all possible categories

$mysqlRows = [

    ["id" => 1, "parent_id" => 0, "title" => "Parent Page"],

    ["id" => 2, "parent_id" => 1, "title" => "Sub Page"],

    ["id" => 3, "parent_id" => 2, "title" => "Sub Sub Page"],

    ["id" => 4, "parent_id" => 0, "title" => "Another Parent Page"],

    ["id" => 5, "parent_id" => 1, "title" => "Sub Page 2"]

];


/*

 * Fill $participatingIds array with id of categories that related to our needs

 * Be aware that $participatingIds has & sign - it means

 * that it will be passed by reference

 */

function collectAllParentsId($id, $mysqlRows, &$participatingIds)

{

    if (!in_array($id, $participatingIds)) {

        $participatingIds[] = $id;

    }

    if ($mysqlRows[$id]["parent_id"] !== 0) {

        collectAllParentsId($mysqlRows[$id]["parent_id"], $mysqlRows, $participatingIds);

    }

}


//Initial function to build a tree from a flat category array

function buildTree(array $elements, $parentId = 0) {

    $branch = array();


    foreach ($elements as $element) {

        if ($element['parent_id'] == $parentId) {

            $children = buildTree($elements, $element['id']);

            if ($children) {

                $element['children'] = $children;

            }

            $branch[] = $element;

        }

    }


    return $branch;

}



/* START */


/*

 * Make array indexes equals to the category's "id",

 * so we can access category like this $mysqlRows[$category_id]

*/

$mysqlRows = array_column($mysqlRows, null, "id");


//Array of ids for which you want create a tree

$someIds = [2, 4, 5];


/*

 * Create one flat array with all Ids that will participating in the tree

 * (leaf id and all of it parents id)

 * Order of ids is doesn't matter here

 * $ids will looks like this:

 * [ 1, 2, 4, 5 ]

 */


$ids = [];

foreach ($someIds as $id) {

    collectAllParentsId($id, $mysqlRows, $ids);

}


//Now filter out all categories that doesn't participating in out tree

$filteredRows = array_filter(

    $mysqlRows,

    function ($key) use ($ids) {

        return (in_array($key, $ids));

    },

    ARRAY_FILTER_USE_KEY

);


//Now we have only desired categories - create the tree from it:

$tree = buildTree($filteredRows);


var_dump($tree);


查看完整回答
反對 回復 2023-11-03
  • 1 回答
  • 0 關注
  • 139 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號