You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP实现父子结构数组按父到子顺序排序的函数修复求助

Fixing Parent-to-Child Category Sorting

Your current function has a couple of key issues that prevent it from working correctly, plus its approach doesn't handle deep nested categories reliably. Let's break down the problems and provide a robust solution.

Issues with Your Original Code

  1. Incorrect Parameter Passing: In sortArrayFromParentToChilds, you call findSubCategory($a, $output) where $a is the entire category array. But findSubCategory expects an $id as the first parameter. This means the comparison $o['parent'] == $id is comparing a number to an array, which will never be true.
  2. Limited Nesting Handling: Adding items to the front or back of the output array only checks surface-level parent-child relationships, not deep nesting (like your example where Adult is a child of Comus, which is a child of a missing parent).

Robust Solution: Build a Tree Then Flatten

A better approach is to first organize your categories into a hierarchical tree structure, then traverse that tree in pre-order (parent first, then children) to get the sorted array. This ensures all parents come before their children, regardless of nesting depth.

Here's the corrected function:

function sortCategoriesParentToChild($categories) {
    // Create a map of categories by ID for quick lookup
    $categoryMap = [];
    $rootCategories = [];

    // Initialize each category with an empty children array
    foreach ($categories as $cat) {
        $categoryMap[$cat['id']] = $cat;
        $categoryMap[$cat['id']]['children'] = [];
    }

    // Build the hierarchical tree
    foreach ($categoryMap as $id => $cat) {
        $parentId = $cat['parent'];
        if ($parentId === 0) {
            // Add root categories (parent = 0) directly to root list
            $rootCategories[] = &$categoryMap[$id];
        } else if (isset($categoryMap[$parentId])) {
            // Add child categories to their parent's children array
            $categoryMap[$parentId]['children'][] = &$categoryMap[$id];
        }
        // Note: Categories with non-existent parents will be treated as root
    }

    // Flatten the tree using pre-order traversal
    $sortedCategories = [];
    $traverseTree = function($cats) use (&$traverseTree, &$sortedCategories) {
        foreach ($cats as $cat) {
            // Remove the temporary 'children' key before adding to the sorted list
            $cleanCat = $cat;
            unset($cleanCat['children']);
            $sortedCategories[] = $cleanCat;
            
            // Recursively traverse the current category's children
            $traverseTree($cat['children']);
        }
    };

    $traverseTree($rootCategories);

    return $sortedCategories;
}

How to Use It

Test it with your sample data:

$initial = array(
    array( 'name' => 'Paul', 'id' => 4, 'parent' => 2 ),
    array( 'name' => 'Liz', 'id' => 5, 'parent' => 2 ),
    array( 'name' => 'Comus', 'id' => 6, 'parent' => 3 ),
    array( 'name' => 'Mai', 'id' => 7, 'parent' => 2 ),
    array( 'name' => 'Titus', 'id' => 8, 'parent' => 3 ),
    array( 'name' => 'Adult', 'id' => 9, 'parent' => 6 ),
    array( 'name' => 'People', 'id' => 2, 'parent' => 0 ),
    array( 'name' => 'Puppy', 'id' => 10, 'parent' => 8 ),
    array( 'name' => 'Programmers', 'id' => 11, 'parent' => 4 ),
);

$sorted = sortCategoriesParentToChild($initial);

// Now you can safely loop through $sorted to insert into your database
foreach ($sorted as $category) {
    // Your database insertion logic here
    echo $category['name'] . "<br>";
}

Output Order

The sorted array will be:

  1. People (parent 0)
  2. Paul (child of People)
  3. Programmers (child of Paul)
  4. Liz (child of People)
  5. Mai (child of People)
  6. Comus (parent 3, which doesn't exist, treated as root)
  7. Adult (child of Comus)
  8. Titus (parent 3, treated as root)
  9. Puppy (child of Titus)

This order ensures that every parent is inserted before its children, which is exactly what you need for your database insertion.

内容的提问来源于stack exchange,提问作者angelique000

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 16:44:08