如何使用PHP将第二个数组值插入第一个数组

时间:2019-02-16 01:42:32

标签: php multidimensional-array

我有一个由第三方供应商提供的多维数组,细节如下。

$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",
            ),
        )
    ),
);

数据输出:https://3v4l.org/uUGsB

我必须根据以下情况,根据“ lessonData”标签中每个“ lessonIdNumber”修改上面数组的结果:将[sessionName]和[teacherData]的值从[lessonData]移到[studentData]数组中的每个[lesson]下基于每个[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 ) ) ) ) ) )

结果数组:https://3v4l.org/lVFlF

我希望我的问题清楚并得到解决。谢谢您的协助。

2 个答案:

答案 0 :(得分:1)

这就是您所要求的...

$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']);

我当然希望这是工作,而不是编码工作。对于新手程序员而言,这是一项非常艰巨的任务,但听起来像是一项很棒的学校作业。

如果是这样,我建议您仅使用它来检查您的工作,否则希望对您有所帮助。祝你好运!

答案 1 :(得分:1)

另一种方法是将课程号最初映射到课程数据。让我们这样尝试吧,

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

工作演示: https://3v4l.org/9JW3J