php group array multi dimensional ( upto third Level)

  arrays, foreach, loops, php

Array subclmnsub column clmn is associated with another array subclmn column rd And subclmn column clmn is clmn’s rd.

1. Array- Clmn

Array
(
    [0] => Array
        (
            [sn] => 1
            [rd] => 2
            [cntnt] => Student
        )
[1] => Array
    (
        [sn] => 2
        [rd] => 2
        [cntnt] => Teacher
    )

[2] => Array
    (
        [sn] => 3
        [rd] => 3
        [cntnt] => Staff
    )

)

2. Array -subclmn

Array
(
    [0] => Array
        (
            [sn] => 1
            [rd] => 1
            [clmn] => 1
            [cntnt] => Rabi
        )
[1] => Array
    (
        [sn] => 2
        [rd] => 2
        [clmn] => 1
        [cntnt] => Kiran
    )

[2] => Array
    (
        [sn] => 3
        [rd] => 3
        [clmn] => 1
        [cntnt] => Neharika
    )

)

3. Array -subclmnsub

Array
(
    [0] => Array
        (
            [sn] => 1
            [rd] => 1
            [clmn] => 1
            [cntnt] => 90%
        )
[1] => Array
    (
        [sn] => 2
        [rd] => 2
        [clmn] => 1
        [cntnt] => 80%
    )

[2] => Array
    (
        [sn] => 3
        [rd] => 3
        [clmn] => 2
        [cntnt] => 70%
    )
[3] => Array
    (
        [sn] => 3
        [rd] => 3
        [clmn] => 2
        [cntnt] => 60%
    )

)

Here requirement is group array in three level based on the clmn of each array. Clmn is top level array, subclmn is second level array and subclmnsub is third level array.

The expected output is

Array
(
    [0] => Array
    (
        [sn] => 1
        [rd] => 2
        [cntnt] => Student
        [subclmn] => Array
            (
                [0] => Array
                    (
                        [sn] => 1
                        [rd] => 1
                        [cntnt] => Rabi
                        [subclmnsub] => Array
                            (
                                [0] => Array
                                    (
                                        [sn] => 1
                                        [rd] => 1
                                        [cntnt] => 90%
                                    )
                                [1] => Array
                                    (
                                        [sn] => 2
                                        [rd] => 2
                                        [cntnt] => 80%
                                    )
                            )
                        
                    )
                [1] => Array
                    (
                        [sn] => 2
                        [rd] => 2
                        [cntnt] => Kiran
                        [subclmnsub] => Array
                            (
                                [0] => Array
                                    (
                                        [sn] => 3
                                        [rd] => 3
                                        [cntnt] => 70%
                                    )
                                [1] => Array
                                    (
                                        [sn] => 4
                                        [rd] => 4
                                        [cntnt] => 60%
                                    )
                            )
                        
                    )
            )
    )
)

First level loop is used clmn array, inside clmn another loop is used for subclmn and last loop is there for subclmnsub.

foreach($clmn AS $z=>$arr){
    $clmn_subclmn[] = $arr; 
    foreach($subclmn AS $y=>$subarr){
        if($arr['rd']==$subarr['clmn'] AND ($arr['typ']==2 OR $arr['typ']==3)){
            $clmn_subclmn[$z]['subclmn'][]=$subarr;
            foreach($subclmnsub AS $x=>$subarrsub){
                if($subarr['rd']==$subarrsub['clmn']){
                    $clmn_subclmn[$z]['subclmn'][$y]['subclmnsub'][]=$subarrsub;
                }
            }
        }
    }
}

But it works for only first index and for other index it is messed up. Why kiran’s subclmnsub is not inserted within kiran array ?

It would be prefer any efficient and shorthand way than foreach loop.

Array
(
    [0] => Array
    (
        [sn] => 1
        [rd] => 2
        [cntnt] => Student
        [subclmn] => Array
            (
                [0] = Array
                    (
                        [sn] => 1
                        [rd] => 1
                        [cntnt] => Rabi
                        [subclmnsub] => Array
                            (
                                [0] => Array
                                    (
                                        [sn] => 1
                                        [rd] => 1
                                        [cntnt] => 90%
                                    )
                                [1] => Array
                                    (
                                        [sn] => 2
                                        [rd] => 2
                                        [cntnt] => 80%
                                    )
                            )
                        
                    )
                [1] => Array
                    (
                        [sn] => 2
                        [rd] => 2
                        [cntnt] => Kiran
                    )
                [3] => Array
                    (
                        [subclmnsub] => Array
                            (
                                [0] => Array
                                    (
                                        [sn] => 3
                                        [rd] => 3
                                        [cntnt] => 70%
                                    )
                                [1] => Array
                                    (
                                        [sn] => 4
                                        [rd] => 4
                                        [cntnt] => 60%
                                    )
                            )
                    )
            )
    )

Source: Ask PHP

LEAVE A COMMENT