【问题标题】:How to return the children of an array under the parent?如何返回父级下的数组的子级?
【发布时间】:2021-09-18 20:27:51
【问题描述】:

有一个数组,数组的每个元素内部都有一个groupId,数组的一些元素有子元素。 如何获得这种输出:

<div class="parentCategory">
   <p>Japanese kitchen</p>
   <div class="childCategory">
      <a class="hasChilds" href="#">Sushi</a>
      <div class="childrenCategory">
         <p>Hot Sushi</p>
         <p>Vegan Sushi</p>
      </div>
      <a href="#">Rolls</a>
      <a href="#">Onigiri</a>
   </div>
</div>

数组中的父类:

[75] => Array
            (
                [groupName] => Japanese kitchen
                [groupId] => 100001
                [hasChilds] => 1
            )

数组中的子类:

[76] => Array
            (
                [groupName] => Sushi
                [groupId] => 101705
                [hasChilds] => 1
                [parentGroupId] => 100001
            )

我只能返回父类别,如何链接子类别,反过来我不知道如何

<? foreach($getGroup['Groups'] as $echoGroup) {
    if($echoGroup['hasChilds'] != null) {
        echo '<li class="parent"><a href="#">' . $echoGroup['groupName'] . '</li>';
    }
} ?>

【问题讨论】:

  • ...如何获得这种输出...请发布您试图实现该目标的代码并描述什么不起作用。
  • 不幸的是,我只能显示没有子类别的父类别,更新了问题
  • 看似迂腐,但关于英语复数的重要说明:“child”的复数形式是“children”,因此您可能希望将 hasChilds 重命名为 hasChildren,这使其符合用英语编写的所有其他与儿童相关的代码。

标签: php arrays filter


【解决方案1】:

我已经考虑了很长时间,我想我会使用 2 个函数来实现。 所以这是整个代码,我还更改了初始数组(添加了一些项目来检查函数的可操作性)。

首先,我创建了一个数组,其中 groupId 是一个键。如果初始数组的键值很重要,那么我们需要执行其他逻辑,但如果不是(我认为它们无关紧要),那么我的建议是创建数组,其中包含名为“ChildrenArray”的子数组,我们将所有子元素。

  1. 列表项

检查一下:

<?php

function AssembleMenu($arrayToAssemble){
    $prepArray=array();
    foreach($arrayToAssemble as $groupId=>$itemArr){
        if(isset($itemArr['parentGroupId']) && $itemArr['parentGroupId'] !=''){
            $prepArray=CheckAndSetChildren($arrayToAssemble, $itemArr);
            $arrayToAssemble=$prepArray;
        }else{
            
        }
    }   
    return($arrayToAssemble);
}

function CheckAndSetChildren($whereToCheck, $WhatToCheck){
    if(array_key_exists($WhatToCheck['parentGroupId'], $whereToCheck)){
        if(!isset($whereToCheck[$WhatToCheck['parentGroupId']]['ChildrenArray'])){
            $whereToCheck[$WhatToCheck['parentGroupId']]['ChildrenArray']=array();
        }
        $whereToCheck[$WhatToCheck['parentGroupId']]['ChildrenArray'][$WhatToCheck['groupId']]=$WhatToCheck;
        if(array_key_exists($WhatToCheck['groupId'], $whereToCheck)){
            unset($whereToCheck[$WhatToCheck['groupId']]);
        }
    }else{
        foreach($whereToCheck as $groupId=>$itemArr){
            if(isset($itemArr['ChildrenArray'])){
                $whereToCheck[$groupId]['ChildrenArray']=CheckAndSetChildren($itemArr['ChildrenArray'], $WhatToCheck);
            }
        }
        if(array_key_exists($WhatToCheck['groupId'], $whereToCheck)){
            unset($whereToCheck[$WhatToCheck['groupId']]);
        }
    }
    return($whereToCheck);
}

$testArray=array(
    73 => array
            (
                'groupName' => 'Lapshichechka',
                'groupId' => 100011,
                'hasChildren' => 1,
                'parentGroupId' => 101708
            ),
    75 => array
            (
                'groupName' => 'Japanese kitchen',
                'groupId' => 100001,
                'hasChildren' => 1
            ),
    76 => array
            (
                'groupName'=> 'Sushi',
                'groupId' => 101705,
                'hasChildren' => 1,
                'parentGroupId' => 100001
            ),
    77 => array
            (
                'groupName'=> 'Rolls',
                'groupId' => 101706,
                'hasChildren' => 0,
                'parentGroupId' => 101705
            ),
    78 => array
            (
                'groupName'=> 'RolledRice',
                'groupId' => 101707,
                'hasChildren' => 0,
                'parentGroupId' => 101705
            ),
    79 => array
            (
                'groupName'=> 'Italian Kitchen',
                'groupId' => 101708,
                'hasChildren' => 0,
            ),
);


$preAssembledArray=array();
$assembledArray=array();


// creating array with groupId as key
foreach($testArray as $arrayId=>$itemProps){
        $preAssembledArray[$itemProps['groupId']]=$itemProps;
}

$assembledArray=AssembleMenu($preAssembledArray);

echo('<br >$assembledArray==<br/ ><br/ >');
print_r($assembledArray);

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多