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 )
)
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
}
---------------------------------EDIT----------------------------------
from the below loop
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
expected 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'
),
)
),
)
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"]);
}