创建多维数组并删除空类别

我有一个类别的一维数组;其中一些是其他类别的子项,其中一些包含“元素”。我需要把它变成一个多维数组,并删除其中没有元素的任何类别或他们的任何孩子(或孩子的孩子......)。


我有以下数组:


$category_array = array(


    1 => array(

        'elementcount' => 3,

        'parentcat' => 0,

        'depth' => 1

    ),


    4 => array(

        'elementcount' => 0,

        'parentcat' => 1,

        'depth' => 2

    ),


    8 => array(

        'elementcount' => 0,

        'parentcat' => 4,

        'depth' => 3

    ),


    9 => array(

        'elementcount' => 2,

        'parentcat' => 4,

        'depth' => 3

    ),


    11 => array(

        'elementcount' => 3,

        'parentcat' => 0,

        'depth' => 1

    ),


    12 => array(

        'elementcount' => 0,

        'parentcat' => 11,

        'depth' => 2

    ),


    21 => array(

        'elementcount' => 3,

        'parentcat' => 0,

        'depth' => 1

    )


);

我需要以下数组:


$multidimensional_array = array(


    1 => array(

        'elementcount' => 3,

        'children' => array(

            4 => array(

                'elementcount' => 0,

                'children' => array(

                    9 => array(

                        'elementcount' => 2

                    )

                )

            )        

        )

    ),


    11 => array(

        'elementcount' => 3,

    ),


    21 => array(

        'elementcount' => 3,

    )


);

如何实现?


潇湘沐
浏览 102回答 2
2回答

交互式爱情

这将是一种方法:<?php$input = [&nbsp; &nbsp; 1 => [&nbsp; &nbsp; &nbsp; &nbsp; 'elementcount' => 3,&nbsp; &nbsp; &nbsp; &nbsp; 'parentcat' => 0,&nbsp; &nbsp; &nbsp; &nbsp; 'depth' => 1&nbsp; &nbsp; ],&nbsp; &nbsp; 4 => [&nbsp; &nbsp; &nbsp; &nbsp; 'elementcount' => 0,&nbsp; &nbsp; &nbsp; &nbsp; 'parentcat' => 1,&nbsp; &nbsp; &nbsp; &nbsp; 'depth' => 2&nbsp; &nbsp; ],&nbsp; &nbsp; 8 => [&nbsp; &nbsp; &nbsp; &nbsp; 'elementcount' => 0,&nbsp; &nbsp; &nbsp; &nbsp; 'parentcat' => 4,&nbsp; &nbsp; &nbsp; &nbsp; 'depth' => 3&nbsp; &nbsp; ],&nbsp; &nbsp; 9 => [&nbsp; &nbsp; &nbsp; &nbsp; 'elementcount' => 2,&nbsp; &nbsp; &nbsp; &nbsp; 'parentcat' => 4,&nbsp; &nbsp; &nbsp; &nbsp; 'depth' => 3&nbsp; &nbsp; ],&nbsp; &nbsp; 11 => [&nbsp; &nbsp; &nbsp; &nbsp; 'elementcount' => 3,&nbsp; &nbsp; &nbsp; &nbsp; 'parentcat' => 0,&nbsp; &nbsp; &nbsp; &nbsp; 'depth' => 1&nbsp; &nbsp; ],&nbsp; &nbsp; 12 => [&nbsp; &nbsp; &nbsp; &nbsp; 'elementcount' => 0,&nbsp; &nbsp; &nbsp; &nbsp; 'parentcat' => 11,&nbsp; &nbsp; &nbsp; &nbsp; 'depth' => 2&nbsp; &nbsp; ],&nbsp; &nbsp; 21 => [&nbsp; &nbsp; &nbsp; &nbsp; 'elementcount' => 3,&nbsp; &nbsp; &nbsp; &nbsp; 'parentcat' => 0,&nbsp; &nbsp; &nbsp; &nbsp; 'depth' => 1&nbsp; &nbsp; ]];$maxDepth = max(array_column($input, 'depth'));// handle elements from higher to lower depthfor ($d = $maxDepth; $d >= 0; $d--) {&nbsp; array_walk($input, function(&$entry, $index) use (&$input, $d) {&nbsp; &nbsp; if (isset($entry['depth']) && $entry['depth'] == $d) {&nbsp; &nbsp; &nbsp; // omit entries without elements or elements in children&nbsp; &nbsp; &nbsp; if ($entry['elementcount'] < 1 && empty($entry['children'])) {&nbsp; &nbsp; &nbsp; &nbsp; unset($input[$index]);&nbsp; &nbsp; &nbsp; // handle as child entry of a parent entry&nbsp; &nbsp; &nbsp; } else if (array_key_exists($entry['parentcat'], $input)) {&nbsp; &nbsp; &nbsp; &nbsp; $input[$entry['parentcat']]['children'][$index] = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'elementcount' => $entry['elementcount'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'children' => isset($entry['children']) ? $entry['children'] : []&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; unset($input[$index]);&nbsp; &nbsp; &nbsp; // handle as ordinary entry&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $input[$index] = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'elementcount' => $entry['elementcount'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'children' => isset($entry['children']) ? $entry['children'] : []&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; });}print_r($input);策略:首先处理更高的深度,以便输入元素的顺序无关紧要对于每个元素,检查父元素是否存在,如果存在,则将其填充在那里重新定义所有处理的元素明显的输出是:(&nbsp; &nbsp; [1] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [elementcount] => 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [children] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [4] => Array&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; [elementcount] => 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [children] => Array&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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [9] => Array&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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [elementcount] => 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [children] => Array&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; &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; &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; &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; )&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [11] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [elementcount] => 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [children] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [21] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [elementcount] => 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [children] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; ))与您的建议相比,我冒昧地创建了一个略有修改的结果:'children' 属性始终作为数组存在。这使得结果的使用在以后更容易和更健壮。我想说的是,一般来说,如果可能的话,结构中的所有元素本身都应该具有相同的结构......

HUWWW

function isParent($id, $list) : bool {&nbsp; &nbsp; foreach($list as $item) {&nbsp; &nbsp; &nbsp; &nbsp; if ($item['parentcat'] === $id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return false;}function buildLevel($parent, $catsByParent) : array {&nbsp; &nbsp; $result = $catsByParent[$parent] ?? [];&nbsp; &nbsp; foreach($result as $id => $cat) {&nbsp; &nbsp; &nbsp; &nbsp; if (isset($catsByParent[$id])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result[$id]['children'] = buildLevel($id, $catsByParent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unset($catsByParent[$id]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $result;}// Filter out empty categories$cats = array_filter(&nbsp; &nbsp; $category_array,&nbsp; &nbsp; function($cat, $id) use($category_array) {&nbsp; &nbsp; &nbsp; &nbsp; return $cat['elementcount']>0 || isParent($id, $category_array);&nbsp; &nbsp; },&nbsp; &nbsp; ARRAY_FILTER_USE_BOTH);$catsByParent = [];// Build cats list keyed by parentcatforeach($cats as $id => $cat) {&nbsp; &nbsp; $parent = $cat['parentcat'];&nbsp; &nbsp; unset($cat['parentcat'], $cat['depth']);&nbsp; &nbsp; $catsByParent[$parent] = ($catsByParent[$parent] ?? []) + [$id => $cat];}// Build result$multidimensional_array = buildLevel(0, $catsByParent);print_r($multidimensional_array);第一个过滤掉空元素,即“其中没有元素或它们的任何子元素的类别”。(children 的 children 要求听起来很奇怪,那不就是再往下一层“他们的孩子”吗?)然后剩余的类别由 parentcat 分组/排序,也就是“级别 id”,以使列表可行 :)。然后遍历该列表,从顶部的级别 id 0 开始,并根据需要递归处理(子级)到最深处。
打开App,查看更多内容
随时随地看视频慕课网APP