第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

創(chuàng)建多維數(shù)組并刪除空類別

創(chuàng)建多維數(shù)組并刪除空類別

PHP
瀟湘沐 2023-04-28 15:53:54
我有一個(gè)類別的一維數(shù)組;其中一些是其他類別的子項(xiàng),其中一些包含“元素”。我需要把它變成一個(gè)多維數(shù)組,并刪除其中沒有元素的任何類別或他們的任何孩子(或孩子的孩子......)。我有以下數(shù)組:$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    ));我需要以下數(shù)組:$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,    ));如何實(shí)現(xiàn)?
查看完整描述

2 回答

?
交互式愛情

TA貢獻(xiàn)1712條經(jīng)驗(yàn) 獲得超3個(gè)贊

這將是一種方法:


<?php


$input = [

    1 => [

        'elementcount' => 3,

        'parentcat' => 0,

        'depth' => 1

    ],

    4 => [

        'elementcount' => 0,

        'parentcat' => 1,

        'depth' => 2

    ],

    8 => [

        'elementcount' => 0,

        'parentcat' => 4,

        'depth' => 3

    ],

    9 => [

        'elementcount' => 2,

        'parentcat' => 4,

        'depth' => 3

    ],

    11 => [

        'elementcount' => 3,

        'parentcat' => 0,

        'depth' => 1

    ],

    12 => [

        'elementcount' => 0,

        'parentcat' => 11,

        'depth' => 2

    ],

    21 => [

        'elementcount' => 3,

        'parentcat' => 0,

        'depth' => 1

    ]

];


$maxDepth = max(array_column($input, 'depth'));


// handle elements from higher to lower depth

for ($d = $maxDepth; $d >= 0; $d--) {


  array_walk($input, function(&$entry, $index) use (&$input, $d) {

    if (isset($entry['depth']) && $entry['depth'] == $d) {


      // omit entries without elements or elements in children

      if ($entry['elementcount'] < 1 && empty($entry['children'])) {

        unset($input[$index]);


      // handle as child entry of a parent entry

      } else if (array_key_exists($entry['parentcat'], $input)) {

        $input[$entry['parentcat']]['children'][$index] = [

          'elementcount' => $entry['elementcount'],

          'children' => isset($entry['children']) ? $entry['children'] : []

        ];

      unset($input[$index]);


      // handle as ordinary entry

      } else {

        $input[$index] = [

          'elementcount' => $entry['elementcount'],

          'children' => isset($entry['children']) ? $entry['children'] : []

        ];

      }

    }

  });


}


print_r($input);

策略:


首先處理更高的深度,以便輸入元素的順序無關(guān)緊要

對于每個(gè)元素,檢查父元素是否存在,如果存在,則將其填充在那里

重新定義所有處理的元素

明顯的輸出是:


(

    [1] => Array

        (

            [elementcount] => 3

            [children] => Array

                (

                    [4] => Array

                        (

                            [elementcount] => 0

                            [children] => Array

                                (

                                    [9] => Array

                                        (

                                            [elementcount] => 2

                                            [children] => Array

                                                (

                                                )

                                        )

                                )

                        )

                )

        )

    [11] => Array

        (

            [elementcount] => 3

            [children] => Array

                (

                )

        )

    [21] => Array

        (

            [elementcount] => 3

            [children] => Array

                (

                )

        )

)

與您的建議相比,我冒昧地創(chuàng)建了一個(gè)略有修改的結(jié)果:


'children' 屬性始終作為數(shù)組存在。這使得結(jié)果的使用在以后更容易和更健壯。我想說的是,一般來說,如果可能的話,結(jié)構(gòu)中的所有元素本身都應(yīng)該具有相同的結(jié)構(gòu)......


查看完整回答
反對 回復(fù) 2023-04-28
?
HUWWW

TA貢獻(xiàn)1874條經(jīng)驗(yàn) 獲得超12個(gè)贊

function isParent($id, $list) : bool {

    foreach($list as $item) {

        if ($item['parentcat'] === $id) {

            return true;

        }

    }


    return false;

}


function buildLevel($parent, $catsByParent) : array {

    $result = $catsByParent[$parent] ?? [];


    foreach($result as $id => $cat) {

        if (isset($catsByParent[$id])) {

            $result[$id]['children'] = buildLevel($id, $catsByParent);

            unset($catsByParent[$id]);

        }

    }


    return $result;

}


// Filter out empty categories

$cats = array_filter(

    $category_array,

    function($cat, $id) use($category_array) {

        return $cat['elementcount']>0 || isParent($id, $category_array);

    },

    ARRAY_FILTER_USE_BOTH

);


$catsByParent = [];

// Build cats list keyed by parentcat

foreach($cats as $id => $cat) {

    $parent = $cat['parentcat'];

    unset($cat['parentcat'], $cat['depth']);

    $catsByParent[$parent] = ($catsByParent[$parent] ?? []) + [$id => $cat];

}


// Build result

$multidimensional_array = buildLevel(0, $catsByParent);


print_r($multidimensional_array);

第一個(gè)過濾掉空元素,即“其中沒有元素或它們的任何子元素的類別”。(children 的 children 要求聽起來很奇怪,那不就是再往下一層“他們的孩子”嗎?)


然后剩余的類別由 parentcat 分組/排序,也就是“級別 id”,以使列表可行 :)。


然后遍歷該列表,從頂部的級別 id 0 開始,并根據(jù)需要遞歸處理(子級)到最深處。


查看完整回答
反對 回復(fù) 2023-04-28
  • 2 回答
  • 0 關(guān)注
  • 125 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號