如何过滤数组以获取 laravel 中两个不同对象中的特定列
how to filter an array to get specific columns in two different objects in laravel
我需要这样的回复。
"result": [
{
"properties": {
"device_id": 15196,
"device_name": Street Light 1,
"state" : 1,
"status": 1,
},
"geometry":{
"lat":33.7017,
"lng": 73.0228
}
},
{
"properties": {
"device_id": 15196,
"device_name": Street Light 1,
"state" : 1,
"status": 1,
},
"geometry":{
"lat":33.7017,
"lng": 73.0228
}
},
]
我的代码在下面。我只想从我的整个回复中分离出两个字段 'lat'、'lng'。我的 sql 查询是正确的,但我想创建上面提到的自定义响应
$get1 = DB::table('device_user')
->join('devices', 'devices.id', '=', 'device_user.device_id')
->join('components', 'devices.id', '=', 'components.device_id')
->select('devices.id as device_id', 'devices.name', 'devices.status', 'components.state', 'components.type', 'devices.lat', 'devices.lng')
->where('device_user.user_id', $request->user_id)
->where('components.type', $type)
->get();
$array = [];
foreach ($get1 as $key => $value) {
array_push($array, ["properties" => $value, "geometry" => $value->lat]);
}
return $array;
试试这个
$get1 = DB::table('device_user')
->join('devices', 'devices.id', '=', 'device_user.device_id')
->join('components', 'devices.id', '=', 'components.device_id')
->select('devices.id as device_id', 'devices.name', 'devices.status', 'components.state', 'components.type', 'devices.lat', 'devices.lng')
->where('device_user.user_id', $request->user_id)
->where('components.type', $type)
->get();
$main_array = [];
foreach ($get1 as $key => $value) {
$main_array[$key]['properties'] = array('device_id' => $value->device_id, 'device_name' => $value->device_name, 'state' => $value->state, 'status' => $value->status);
$main_array[$key]['geometry'] = array('lat' => $value->lat, 'lng' => $value->lng);
}
return $main_array;
我试了很多次,终于搞定了。
$get = DB::table('device_user')
->join('devices', 'devices.id', '=', 'device_user.device_id')
->join('components', 'devices.id', '=', 'components.device_id')
->select('devices.id as device_id', 'devices.name', 'devices.status', 'components.state', 'components.type', 'devices.lat', 'devices.lng')
->where('device_user.user_id', $request->user_id)
->where('components.type', $type)
->get();
$finalarray =[];
foreach ($get as $key => $value) {
array_push($finalarray, ["properties" => ['device_id' => $value->device_id, 'name' => $value->name, 'status' => $value->status, 'state' => $value->state, 'type' => $value->type, ], "geometry" => ["coordinates" => ['lat' => $value->lat, 'lng' => $value->lng]]]);
}
我需要这样的回复。
"result": [
{
"properties": {
"device_id": 15196,
"device_name": Street Light 1,
"state" : 1,
"status": 1,
},
"geometry":{
"lat":33.7017,
"lng": 73.0228
}
},
{
"properties": {
"device_id": 15196,
"device_name": Street Light 1,
"state" : 1,
"status": 1,
},
"geometry":{
"lat":33.7017,
"lng": 73.0228
}
},
]
我的代码在下面。我只想从我的整个回复中分离出两个字段 'lat'、'lng'。我的 sql 查询是正确的,但我想创建上面提到的自定义响应
$get1 = DB::table('device_user')
->join('devices', 'devices.id', '=', 'device_user.device_id')
->join('components', 'devices.id', '=', 'components.device_id')
->select('devices.id as device_id', 'devices.name', 'devices.status', 'components.state', 'components.type', 'devices.lat', 'devices.lng')
->where('device_user.user_id', $request->user_id)
->where('components.type', $type)
->get();
$array = [];
foreach ($get1 as $key => $value) {
array_push($array, ["properties" => $value, "geometry" => $value->lat]);
}
return $array;
试试这个
$get1 = DB::table('device_user')
->join('devices', 'devices.id', '=', 'device_user.device_id')
->join('components', 'devices.id', '=', 'components.device_id')
->select('devices.id as device_id', 'devices.name', 'devices.status', 'components.state', 'components.type', 'devices.lat', 'devices.lng')
->where('device_user.user_id', $request->user_id)
->where('components.type', $type)
->get();
$main_array = [];
foreach ($get1 as $key => $value) {
$main_array[$key]['properties'] = array('device_id' => $value->device_id, 'device_name' => $value->device_name, 'state' => $value->state, 'status' => $value->status);
$main_array[$key]['geometry'] = array('lat' => $value->lat, 'lng' => $value->lng);
}
return $main_array;
我试了很多次,终于搞定了。
$get = DB::table('device_user')
->join('devices', 'devices.id', '=', 'device_user.device_id')
->join('components', 'devices.id', '=', 'components.device_id')
->select('devices.id as device_id', 'devices.name', 'devices.status', 'components.state', 'components.type', 'devices.lat', 'devices.lng')
->where('device_user.user_id', $request->user_id)
->where('components.type', $type)
->get();
$finalarray =[];
foreach ($get as $key => $value) {
array_push($finalarray, ["properties" => ['device_id' => $value->device_id, 'name' => $value->name, 'status' => $value->status, 'state' => $value->state, 'type' => $value->type, ], "geometry" => ["coordinates" => ['lat' => $value->lat, 'lng' => $value->lng]]]);
}