php多维组数组(高达第三级)

8cdiaqws  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(278)

排列 subclmnsub 纵队 clmn 与另一个数组关联 subclmn 纵队 rdsubclmn 纵队 clmn 是clmn的吗 rd .
1.数组-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.阵列-子LMN

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.阵列-子LMNSUB

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

)
这里的需求是基于 clmn 每个数组的。 Clmn 是顶级数组, subclmn 是二级数组和 subclmnsub 是三级数组。
预期产量为

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

                    )
            )
    )
)

采用一级回路 clmn 阵列,内部 clmn 另一个循环用于 subclmn 最后一个循环是为 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;
                }
            }
        }
    }
}

但它只对第一个索引有效,而对其他索引则是一团糟。为什么kiran subclmnsub 是否未插入kiran数组中?
比起foreach循环,它更喜欢任何高效和快捷的方式。

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

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题