如何在laravel中合并两个相似的对象

please i need help here. I have two similar objects and i want to merge them into one object in laravel. How is it done?

Here are my objects

{"course_code":"UGRC110","venue_id":22,"exam_date":"May 6, 2017","exam_time":"3:30 pm","student_no":400}

and

{"course_code":"UGRC110","venue_id":25,"exam_date":"May 6, 2017","exam_time":"3:30 pm","student_no":700}

I want to merge and get something like this

{"course_code":"UGRC110","venue_id":[22,25],"exam_date":"May 6, 2017","exam_time":"3:30 pm","student_no":[400,700]}

I want to get the venue_id and student_no merged together.. any help will be much appreciated.. Thank you

Hope this will help you out. Here we are using just simple foreach to merge these two json's.

Try this code snippet here

<?php

$json1 = '{"course_code":"UGRC110","venue_id":22,"exam_date":"May 6, 2017","exam_time":"3:30 pm","student_no":400}';
$json2 = '{"course_code":"UGRC110","venue_id":25,"exam_date":"May 6, 2017","exam_time":"3:30 pm","student_no":700}';

json_merge($json1, $json2);
function json_merge($json1, $json2)
{
    $array1 = json_decode($json1, true);
    $array2 = json_decode($json2, true);
    $result = array();
    foreach ($array1 as $key => $value)
    {
        if ($array1[$key] == $array2[$key])
        {
            $result[$key] = $array2[$key];
        } else
        {
            $result[$key][] = $array1[$key];
            $result[$key][] = $array2[$key];
        }
    }
    return json_encode($result, JSON_PRETTY_PRINT);
}

Output:

{
    "course_code": "UGRC110",
    "venue_id": [
        22,
        25
    ],
    "exam_date": "May 6, 2017",
    "exam_time": "3:30 pm",
    "student_no": [
        400,
        700
    ]
}