PHP数组组循环并创建多维数组 [英] PHP array group in loop and create multi dimensional array

查看:59
本文介绍了PHP数组组循环并创建多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的关联多维数组

I have an associative multi dimensional array as below

$data = array();
$data = Array ( 
    [0] => Array ( [class] => 1styear [branch] => IT [Exam] => SEM1 [student name] => Alex [Bio] => Good Boy )
    [1] => Array ( [class] => 2ndyear [branch] => Finance [Exam] => SEM1 [student name] => Mark [Bio] => Intelligent )
    [2] => Array ( [class] => 2ndyear [branch] => IT [Exam] => SEM1 [student name] => Shaun [Bio] => Football Player ) 
    [3] => Array ( [class] => 1styear [branch] => Finance [Exam] => SEM2 [student name] => Mike [Bio] => Sport Player ) 
    [4] => Array ( [class] => 1styear [branch] => IT [Exam] => SEM2 [student name] => Martin [Bio] => Smart  )
    [5] => Array ( [class] => 1styear [branch] => IT [Exam] => SEM1 [student name] => Philip [Bio] => Programmer  )
    )

我需要基于上述数组中的相似元素创建新数组.意味着我必须创建数组组.例如,class元素具有重复的1styear和2ndyear值.因此,它应该创建唯一元素的数组.再说一遍,班级是父级数组,班级级数组内应有基于分支的数组,而brance Exam数组内应有,而Exam数组内应有学生姓名和简历的关联数组.

I need to create new array based on similar element from above array. means I have to create array group. for e.g class element has repetitive 1styear and 2ndyear value. so it shouls create array of unique element. then again class is parent array and inside class array there should be branch based array and inside brance Exam array and inside Exam array there should be associative array of student name and bio.

所以基本上数组应该看起来像这样

so basically array should look like this

array(
    "1styear" => array(
        "IT" => array(
            "SEM1" => array(
                array(
                    "student name" => "Alex",
                    "Bio" => "Good Boy"
                ),
                array(
                    "student name" => "Philip",
                    "Bio" => "Programmer"
                )
            ),
            "SEM2" => array(
                array(
                    "student name" => "Martin",
                    "Bio" => "Smart"
                )
            )
        )
    ),
    "2ndyear" => array(
        "Finance" => array(
            "SEM1" => array(
                array(
                    "student name" => "Mark",
                    "Bio" => "Intelligent"
                )
            ),
            "SEM2" => array(
                array(
                    "student name" => "Mike",
                    "Bio" => "Sport Player"
                )
            )
        )
    )
);

要基于类创建组,我在下面做的很好,但是如何在该类中创建数组

To make group based on class i did like below which is working fine but how to create array inside that

$classgroup = array();  
    foreach($data as $inarray){

         $classgroup[$inarray['class']][] = $inarray;
    }
    $classarray = array();
    foreach($classgroup as $key => $value){
            echo $key; // output is 1styear and secondyear 
            create array like above
    }

---------------------------------编辑---------- ------------------------

来自下面的循环

foreach($data as $array){
        $grouped[$array["class"]][$array["branch"]][$array["Exam"]][]=array("student name"=>$array["student name"],"Bio"=>$array["Bio"]);
} 

我的期望值是 但是如果我需要另一个这样的

i got expected o/p but if i need another o/p like this

预期o/p

array(
    '1styear' =>
        array (
            0 =>
                array(
                    'Exam' => 'SEM1',
                    'branch' =>
                        array (
                            0 => 'IT'
                        ),
                ),
            1 =>
                array(
                    'Exam' => 'SEM2',
                    'branch' =>
                        array (
                            0 => 'IT'
                        ),
                ),
        ),
    '2ndyear' =>
        array (
            0 =>
                array(
                    'Exam' => 'SEM1',
                    'branch' =>
                        array (
                            0 => 'Finance',
                        ),
                ),
            1 =>
                array(
                    'Exam' => 'SEM2',
                    'branch' =>
                        array (
                            0 => 'Finance'
                        ),
                )
        ),
)

我尝试了以下循环,但未如预期那样获得o/p

i tried following loop but not getting o/p as expected

foreach($data as $array){
        $grouped[$array["class"]][]=array("Exam"=>$array["Exam"],"branch"=>$array["branch"]);
}

推荐答案

单线循环!

foreach($data as $array){
        $grouped[$array["class"]][$array["branch"]][$array["Exam"]][]=array("student name"=>$array["student name"],"Bio"=>$array["Bio"]);
}

$grouped产生:

Array(
    [1styear] => Array(
        [IT] => Array(
            [SEM1] => array(
                [0] => array(
                    [student name] => Alex,
                    [Bio] => Good Boy
                ),
                [1] => array(
                    [student name] => Philip,
                    [Bio] => Programmer
                )
            ),
            [SEM2] => array(
                [0] => array(
                    [student name] => Martin,
                    [Bio] => Smart
                )
            )
        ),
        [Finance] => array(
            [SEM2] => array(
                [0] => array(
                    [student name] => Mike,
                    [Bio] => Sport Player
                )
            )
        )
    ),
    [2ndyear] => array(
        [Finance] => array(
            [SEM1] => array(
                [0] => array(
                    [student name] => Mark,
                    [Bio] => Intelligent
                )
            )
        ),
        [IT] => array(
            [SEM1] => array(
                [0] => array(
                    [student name] => Shaun,
                    [Bio] => Football Player
                )
            )
        )
    )
)


您的跟进案例更加有趣/富有挑战性.我不得不淘汰一些我不经常使用的功能.检查一下:


Your follow up case, was MUCH more fun/challenging. I had to knock the dust off of some functions I don't play with very often. Check this out:

<?php
$data = array ( 
    array ( "class"=>"1styear","branch"=>"IT","Exam"=>"SEM1","student name"=>"Alex","Bio"=>"Good Boy"),
    array ( "class"=>"2ndyear","branch"=>"Finance","Exam"=>"SEM1","student name"=>"Mark","Bio"=>"Intelligent" ),
    array ( "class"=>"2ndyear", "branch"=>"IT","Exam"=>"SEM1","student name"=>"Shaun","Bio"=>"Football Player" ), 
    array ( "class"=>"1styear","branch"=>"Finance","Exam"=>"SEM2","student name"=>"Mike","Bio"=>"Sport Player" ), 
    array ( "class"=>"1styear","branch"=>"IT","Exam"=>"SEM2","student name"=>"Martin","Bio"=>"Smart"),
    array ( "class"=>"1styear","branch"=>"IT","Exam"=>"SEM1","student name"=>"Philip","Bio"=>"Programmer"  )
);
$class_keys=array_unique(array_column($data,"class"));  // create array of unique class values
$Exam_keys=array_unique(array_column($data,"Exam"));  // create array of unique Exam values
foreach($class_keys as $class_key){
    $i=0;  // "class" subarray index
    foreach($Exam_keys as $Exam_key){
        $q=array("class"=>$class_key,"Exam"=>$Exam_key);  // this array can have 1 or more pairs
        // create an array only of rows where $q's key-value pairs exist
        $qualifying_array=array_filter(
            $data,
            function($val)use($q){  
                if(count(array_intersect_assoc($val,$q))==count($q)){  // total pairs found = total pairs sought
                    return $val;
                }
            },
            ARRAY_FILTER_USE_BOTH
        );
        foreach($qualifying_array as $qa){  // push appropriate values into array
            $grouped2[$class_key][$i]["Exam"]=$qa["Exam"];
            $grouped2[$class_key][$i]["branch"][]=$qa["branch"];
        }
        if(isset($grouped2[$class_key][$i]["branch"])){  // ensure no duplicate values in "branch" subarray
            $grouped2[$class_key][$i]["branch"]=array_unique($grouped2[$class_key][$i]["branch"]);
        }
        ++$i;  // increment the index for each "class" subarray
    }
}
echo "<pre>";
print_r($grouped2);
echo "</pre>";

输出与您请求的输出不同,但是我认为您只是在显示它的总体外观.如果不太正确,请告诉我.

The output isn't identical to what you requested, but I think you were just showing what it should look like generally. If this isn't quite right, let me know.

array(
    [1styear]=>array(
        [0]=>array(
            [Exam]=>SEM1
            [branch]=>array(
                [0]=>IT
            )
        ),
        [1]=>array(
            [Exam]=>SEM2
            [branch]=>array(
                [0]=>Finance,
                [1]=>IT
            )
        )
    ),
    [2ndyear]=>array(
        [0]=>array(
            [Exam]=>SEM1
            [branch]=>array(
                [0]=>Finance,
                [1]=>IT
            )
        )
    )
)

这篇关于PHP数组组循环并创建多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆