Php 如何在laravel中合并两个相似的对象
求求你,我需要帮助。我有两个相似的对象,我想在laravel中将它们合并成一个对象。怎么做的 这是我的物品Php 如何在laravel中合并两个相似的对象,php,json,laravel,object,Php,Json,Laravel,Object,求求你,我需要帮助。我有两个相似的对象,我想在laravel中将它们合并成一个对象。怎么做的 这是我的物品 {"course_code":"UGRC110","venue_id":22,"exam_date":"May 6, 2017","exam_time":"3:30 pm","student_no":400} 及 我想合并得到这样的东西 {"course_code":"UGRC110","venue_id":[22,25],"exam_date":"May 6, 2017","exam_
{"course_code":"UGRC110","venue_id":22,"exam_date":"May 6, 2017","exam_time":"3:30 pm","student_no":400}
及
我想合并得到这样的东西
{"course_code":"UGRC110","venue_id":[22,25],"exam_date":"May 6, 2017","exam_time":"3:30 pm","student_no":[400,700]}
我想把场地号和学生号合并在一起。。任何帮助都将是免费的
非常感谢。。谢谢希望这能帮到你。在这里,我们只使用simple
foreach
来合并这两个json
你试过什么?看起来你应该先进行json解码,然后进行数组合并($json1['venture\u id'],$json2['venture\u id'])和数组合并($json1['student\u no'],$json2['student\u no'])。。太好了。。谢谢@Sahil。。。。。。无论如何,请不要他们有任何拉维收集方法,可以执行类似的功能,而无需我循环的对象。我想要一个非常有效的方法,因为我会将它应用到我数据库中的数千条记录上database@Ernestolaravel没有一种方法可以做到这一点。我想,您可以将我的代码更改为php函数,该函数可以多次使用。@Ernesto您可以这样做。。。我已经编辑了我的帖子,希望能对你有所帮助……再次感谢@Sahil
{"course_code":"UGRC110","venue_id":[22,25],"exam_date":"May 6, 2017","exam_time":"3:30 pm","student_no":[400,700]}
<?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);
}
{
"course_code": "UGRC110",
"venue_id": [
22,
25
],
"exam_date": "May 6, 2017",
"exam_time": "3:30 pm",
"student_no": [
400,
700
]
}