Skip to content
Advertisement

How to return the children of an array under the parent?

There is an array, inside each element of the array there is a groupId, some elements of the array have children. How to get this kind of output:

<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>

Parent category in array:

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

Children category in array:

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

I was able to only return parent categories, how to link child categories, in turn I could not figure out how

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

Advertisement

Answer

I’ve been thinking about for a rather long time and I think I would do it using 2 functions. So here’s the whole code, I also changed the initial array (added a few more items to check functions workability).

First of all, I create an array where groupId is a key. I if the initial array’s key values are important, then we’ll need to do the other logics, but if not (and I suppose they are not matter), then my suggestion is to create array whith subarrays named “ChildrenArray” where we put all children elements.

  1. List item

Check it out:

<?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);

?>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement