skip to Main Content

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


  1. This does what you request…

    $lessonData  = $data['lessonData'];
    $studentData = $data['studentData'];
    
    $lessons = array();
    
    foreach ($lessonData as $lessonDat)
    {
        $lessons[$lessonDat['lessonId']['lessonIdNumber']] = $lessonDat;
    }
    
    foreach ($studentData as $id=>$studentDat) {
    
        $studentData[$id]['sessionName'] = $lessons[$studentDat['lesson']['lessonId']['lessonIdNumber']]['sessionName'];
        $studentData[$id]['teacherData'] = $lessons[$studentDat['lesson']['lessonId']['lessonIdNumber']]['teacherData'];
    }
    
    unset($data['lessonData']);
    

    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!

    Login or Signup to reply.
  2. Another way to do it with initially mapping the lesson number to lesson data. Let’s try like this way,

    $student = $data['studentData'];
    $lesson = $data['lessonData'];
    $expected = [];
    
    # mapping the lession id to lession data e.g 40=>[sessionName=>'Coreldraw','teacherData'=>[array]]
    foreach($lesson as $key=>$value){
        $mapped[$value['lessonId']['lessonIdNumber']] = ['sessionName'=>$value['sessionName'],'teacherData'=>$value['teacherData']];
    }
    
    foreach($student as $k=>$v){
        $lesson_id = $v['lesson']['lessonId']['lessonIdNumber'];
        if(array_key_exists($lesson_id, $mapped)){
            $expected['studentData'][$k] = array_merge($student[$k], $mapped[$lesson_id]);
        }
    }
    
    print_r($expected);
    

    WORKING DEMO: https://3v4l.org/9JW3J

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search