Laravel - 在 PHP 中建立物件數組和數組
P粉298305266
P粉298305266 2024-03-30 17:17:36
0
1
499

我正在嘗試在 laravel php 中建立一個物件陣列。到目前為止我已經實現了這個目標。

當我返回數組時,我得到了回應,為了清楚起見,我添加了圖像。

我得到這個輸出

我想創建這樣的回應...

[
    {
        "student": "Jaime Thomas",
        "subjects": [
            {
                "subject": "Physics",
                "marks_": 0
            },
            {
                "subject": "Chemistry",
                "marks_": 0
            }
        ]
    },
    {
        "student": "Jaime Dey",
        "subjects": [
            {
                "subject": "Physics",
                "marks_": 0
            },
            {
                "subject": "Chemistry",
                "marks_": 0
            }
        ]
    }
]
$usersData = User::where('user_id', 2)->where("stud_class", $exam_details->exam_class_id)->where("XXXX", $exam_details->exam_branch_id)->get();
            foreach ($exam_data as  $subject) {
                $att_exams =  MODEL::where('XXXXX', $subject->subject_id)
                    ->where('XXXX', $user->id)
                    ->first();
                if ($att_exams) {
                    $marks =  MODEL::where('XXXX', $att_exams->attended_exams_id)->get();
                    $right = 0;
                    $wrong = 0;
                    $total_marks = $marks->sum('XXXX');
                    
                    $total_negative_marks = $wrong * $subject->negative_marks;
                    $subjectsArray[] = array(
                        "subject" => $subject->subject_name,
                        "marks_" => $total_marks - $total_negative_marks,
                    );
                } else {
                    $subjectsArray[] = array(
                        "subject" => $subject->subject_name,
                        "marks_" => 0,
                    );
                }
            }
            $studentsArray["subjects"] = array($subjectsArray);
        }
        return $studentsArray;

P粉298305266
P粉298305266

全部回覆(1)
P粉921130067

您可以嘗試以下程式碼:

$studentsArray = [];
        $usersData = User::where('user_id', 2)->where("stud_class", $exam_details->exam_class_id)->where("XXXX", $exam_details->exam_branch_id)->get(['name', 'id']);
        foreach ($usersData as $user) {
            $subjectsArray = [];
            foreach ($exam_data as  $subject) {
                $att_exams =  MODEL::where('XXXXX', $subject->subject_id)
                    ->where('XXXX', $user->id)
                    ->first();
                if ($att_exams) {
                    $marks =  MODEL::where('XXXX', $att_exams->attended_exams_id)->get();
                    $right = 0;
                    $wrong = 0;
                    $total_marks = $marks->sum('XXXX');
                    foreach ($marks as $mark) {
                        if ($mark->XXX== 0) {
                            $wrong++;
                        } else {
                            $right++;
                        }
                    }
                    $total_negative_marks = $wrong * $subject->negative_marks;
                    $subjectsArray[] = [
                        "subject" => $subject->subject_name,
                        "marks_" => $total_marks - $total_negative_marks,
                    ];
                } else {
                    $subjectsArray[] = array(
                        "subject" => $subject->subject_name,
                        "marks_" => 0,
                    );
                }
            }
            $studentsArray[] = [
                "student" => $user->name,
                "subjects" => $subjectsArray
            ];
        }
        return $studentsArray;
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板