我遇到了从以下代码获取JSON对象数组的问题。
public function staffStatusFromSameDept($deptID)
{
$sameDeptEmployee=StaffsModel::where("DepartmentID","=",$deptID)->get();
$count=$sameDeptEmployee->count();
if ($count>0)
{
foreach ($sameDeptEmployee as $value)
{
$data=DB::table('status')
->join('staffs','status.StaffPin', '=', 'staffs.StaffPin')
->select('staffs.StaffName','staffs.DesignationName',
'status.staffStatus', 'status.currentLocation',
'staffs.EmailID','staffs.MobileNO','status.returnTime','staffs.Photo' )
->where('staffs.StaffPin','=',$value->StaffPin)
->get();
$response[]=$data;
}
}
else{
$response=["error" => "Invalid Department ID"];
}
header('Content-type: application/json');
echo json_encode($response);
}
实际输出:
[[{" StaffName":" Mamun hosen"," DesignationName":" PO(MF)",&#34 ; staffStatus":"工作"" currentLocation":"朗布尔"" EMAILID":"马蒙@布拉克。净"" MobileNO":" 01716340278"" returnTime":" 04:30""电话&#34 ;:" helal.jpg"}],[{" StaffName":" nahid 哈桑"" DesignationName":" PO(MF)"" staffStatus":"工作"" currentLocation& #34;:"朗布尔"" EMAILID":" nahid@brac.net"," MobileNO":" 01716340278&# 34;," returnTime":" 04:30""电话":" helal.jpg"}]]
预期产出:
[{" StaffName":" Mamun hosen"," DesignationName":" PO(MF)"," staffStatus":"工作"" currentLocation":"朗布尔"" EMAILID":" mamun@brac.net& #34;" MobileNO":" 01716340278"" returnTime":" 04:30""电话" :" helal.jpg"},{" StaffName":" nahid 哈桑"" DesignationName":" PO(MF)"" staffStatus":"工作"" currentLocation& #34;:"朗布尔"" EMAILID":" nahid@brac.net"," MobileNO":" 01716340278&# 34;," returnTime":" 04:30""电话":" helal.jpg"}]
我的代码中的问题是每个人详细记录了JSON对象数组。但是我想要一个JSON对象数组以及我将从这个JSON对象数组获得的每个人的详细信息。
答案 0 :(得分:0)
因为->get()
将返回找到的记录数组。
因此,如果您的查询始终有1条记录,请将->get()
更改为->first()
,否则请使用$response = array_merge($response, $data)
代替$response[] = $data
答案 1 :(得分:0)
您只需将$response[]=$data;
更改为$response=$data;