I have multidimensional array given from thirth party supplier with detail as bellow.
$data = array ( "studentData" => array ( array( "university" => array( "name" => "Tes Name", "address" => "Street name", "province" => "Province name", ), "college" => array( "name" => "IT" ), "examDate" => "2019-01-20", "lesson" => array( "lessonId" => array( "lessonIdNumber" => "40" ) ), "countStudent" => "2", "examResult" => array( array( "studentGender" => "Male", "studentName" => array( array( "name" => "George", "result" => "98" ), array( "name" => "Leonard", "result" => "84" ), ), ) ) ), array( "university" => array( "name" => "Tes Name", "address" => "Street name", "province" => "Province name", ), "college" => array( "name" => "IT" ), "examDate" => "2019-01-20", "lesson" => array( "lessonId" => array( "lessonIdNumber" => "41" ) ), "countStudent" => "1", "examResult" => array( "studentGender" => "Female", "examDate" => "2019-01-20", "lessonId" => array( "lessonIdNumber" => "40" ), "countStudent" => "1", "studentName" => array( array( "name" => "Anita", "result" => "71" ) ), ) ), array( "university" => array( "name" => "Tes Name", "address" => "Street name", "province" => "Province name", ), "college" => array( "name" => "Design Graphics" ), "examDate" => "2019-01-20", "lesson" => array( "lessonId" => array( "lessonIdNumber" => "42" ) ), "countStudent" => "3", "examResult" => array( array( "studentGender" => "Male", "studentName" => array( array( "name" => "Edward", "result" => "83" ), array( "name" => "William", "result" => "86" ), array( "name" => "Oliver", "result" => "77" ), ), ) ) ), array( "university" => array( "name" => "Tes Name", "address" => "Street name", "province" => "Province name", ), "college" => array( "name" => "Design Graphics" ), "examDate" => "2019-01-20", "lesson" => array( "lessonId" => array( "lessonIdNumber" => "42" ) ), "countStudent" => "2", "examResult" => array( array( "studentGender" => "Female", "studentName" => array( array( "name" => "Isabel", "result" => "77" ), array( "name" => "Dorothi", "result" => "86" ) ), ), ) ), ), "lessonData" => array ( array( "lessonId" => array( "lessonIdNumber" => "40" ), "sessionName" => "PHP and Array", "teacherData" => array( "teacherName" => "Teacher Name One", "teacherId" => "229910", ), ), array( "lessonId" => array( "lessonIdNumber" => "41" ), "sessionName" => "Photoshop", "teacherData" => array( "teacherName" => "Teacher Name Two", "teacherId" => "981881", ), ), array( "lessonId" => array( "lessonIdNumber" => "42" ), "sessionName" => "Coreldraw", "teacherData" => array( "teacherName" => "Teacher Name Three", "teacherId" => "327718", ), ) ), );
Data output : https://3v4l.org/uUGsB
I have to modify result from above array base on every “lessonIdNumber” from “lessonData” tag with scenario : move value of [sessionName] and [teacherData] from [lessonData] into bellow each [lesson] in [studentData] array base on each [lessonIdNumber].
Array
(
[studentData] => Array
(
[0] => Array
(
[university] => Array
(
[name] => Tes Name
[address] => Street name
[province] => Province name
)
[college] => Array
(
[name] => IT
)
[examDate] => 2019-01-20
[lesson] => Array
(
[lessonId] => Array
(
[lessonIdNumber] => 40
)
)
[sessionName] => PHP and Array
[teacherData] => Array
(
[teacherName] => Teacher Name One
[teacherId] => 229910
)
[countStudent] => 2
[examResult] => Array
(
[0] => Array
(
[studentGender] => Male
[studentName] => Array
(
[0] => Array
(
[name] => George
[result] => 98
)
[1] => Array
(
[name] => Leonard
[result] => 84
)
)
)
)
)
[1] => Array
(
[university] => Array
(
[name] => Tes Name
[address] => Street name
[province] => Province name
)
[college] => Array
(
[name] => IT
)
[examDate] => 2019-01-20
[lesson] => Array
(
[lessonId] => Array
(
[lessonIdNumber] => 41
)
)
[sessionName] => Photoshop
[teacherData] => Array
(
[teacherName] => Teacher Name Two
[teacherId] => 981881
)
[countStudent] => 1
[examResult] => Array
(
[studentGender] => Female
[examDate] => 2019-01-20
[lessonId] => Array
(
[lessonIdNumber] => 40
)
[countStudent] => 1
[studentName] => Array
(
[0] => Array
(
[name] => Anita
[result] => 71
)
)
)
)
[2] => Array
(
[university] => Array
(
[name] => Tes Name
[address] => Street name
[province] => Province name
)
[college] => Array
(
[name] => Design Graphics
)
[examDate] => 2019-01-20
[lesson] => Array
(
[lessonId] => Array
(
[lessonIdNumber] => 42
)
)
[sessionName] => Coreldraw
[teacherData] => Array
(
[teacherName] => Teacher Name Three
[teacherId] => 327718
)
[countStudent] => 3
[examResult] => Array
(
[0] => Array
(
[studentGender] => Male
[studentName] => Array
(
[0] => Array
(
[name] => Edward
[result] => 83
)
[1] => Array
(
[name] => William
[result] => 86
)
[2] => Array
(
[name] => Oliver
[result] => 77
)
)
)
)
)
[3] => Array
(
[university] => Array
(
[name] => Tes Name
[address] => Street name
[province] => Province name
)
[college] => Array
(
[name] => Design Graphics
)
[examDate] => 2019-01-20
[lesson] => Array
(
[lessonId] => Array
(
[lessonIdNumber] => 42
)
)
[sessionName] => Coreldraw
[teacherData] => Array
(
[teacherName] => Teacher Name Three
[teacherId] => 327718
)
[countStudent] => 2
[examResult] => Array
(
[0] => Array
(
[studentGender] => Female
[studentName] => Array
(
[0] => Array
(
[name] => Isabel
[result] => 77
)
[1] => Array
(
[name] => Dorothi
[result] => 86
)
)
)
)
)
)
)
Result array : https://3v4l.org/lVFlF
I hope my question is clear and get solution. Thank you for your assistance.
2
Answers
This does what you request…
I certainly do hope this is for work and not a coding assignment. It’s a pretty challenging task for a novice programmer but sounds like an awesome school assignment.
If so, I’d recommend you just use this to check your work, otherwise I hope it is of help. Best of luck!
Another way to do it with initially mapping the lesson number to lesson data. Let’s try like this way,
WORKING DEMO: https://3v4l.org/9JW3J