PHP array group in loop and create multi dimensional array

后端 未结 2 1872
隐瞒了意图╮
隐瞒了意图╮ 2021-01-19 07:12

I have an associative multi dimensional array as below

$data = array();
$data = Array ( 
    [0] => Array ( [class] => 1styear [branch] => IT [Exam]         


        
2条回答
  •  盖世英雄少女心
    2021-01-19 08:10

    A one-liner in a loop!

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

    $grouped produces:

    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:

    "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 "
    ";
    print_r($grouped2);
    echo "
    ";

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

提交回复
热议问题