如何根据键名条件从集合中获取价值?
How to get value from a collection base on key name condition?
我正在尝试从 PHP 中的集合中获取值。
$todaylog
变量包含来自 laravel 查询构建器的集合:
$todaylog = [
{
"row_id":55,
"emp_number":"IPPH0004",
"timestamp":"03:30:23",
"attendance_status":"Punch In",
"date_created":"2021-10-01"
},
{
"row_id":56,
"emp_number":"IPPH0004",
"timestamp":"11:32:50",
"attendance_status":"Start Break",
"date_created":"2021-10-01"
},
{
"row_id":57,
"emp_number":"IPPH0004",
"timestamp":"11:33:09",
"attendance_status":"End Break",
"date_created":"2021-10-01"
}
]
到目前为止我做了什么:但是这种方法太慢了:
$timein = DB::table('attendance')->select('timestamp')->where('attendance_status','Punch In')->get();
现在我想要这样的东西:(PS这只是一个伪代码)
$timein = (where) $todaylog.attendance_status = "Punch In"
$endbreak = (where) $todaylog.attendance_status = "End Break"
这可能吗?或者我必须将它们单独查询到数据库?谢谢
可以使用laravel集合where方法:
collect($todaylog)->where("attendance_status", "Punch In")->first();
Laravel 你需要的是这样的东西:
$rows = collect([
[
"row_id" => 55,
"emp_number" => "IPPH0004",
"timestamp" => "03:30:23",
"attendance_status" => "Punch In",
"date_created" => "2021-10-01"
],
[
"row_id" => 56,
"emp_number" => "IPPH0004",
"timestamp" => "11:32:50",
"attendance_status" => "Start Break",
"date_created" => "2021-10-01"
],
[
"row_id" => 57,
"emp_number" => "IPPH0004",
"timestamp" => "11:33:09",
"attendance_status" => "End Break",
"date_created" => "2021-10-01"
]]);
dd($rows->where('row_id', 57));
结果:
Illuminate\Support\Collection {#446 ▼
#items: array:1 [▼
2 => array:5 [▼
"row_id" => 57
"emp_number" => "IPPH0004"
"timestamp" => "11:33:09"
"attendance_status" => "End Break"
"date_created" => "2021-10-01"
]
]
}
我正在尝试从 PHP 中的集合中获取值。
$todaylog
变量包含来自 laravel 查询构建器的集合:
$todaylog = [
{
"row_id":55,
"emp_number":"IPPH0004",
"timestamp":"03:30:23",
"attendance_status":"Punch In",
"date_created":"2021-10-01"
},
{
"row_id":56,
"emp_number":"IPPH0004",
"timestamp":"11:32:50",
"attendance_status":"Start Break",
"date_created":"2021-10-01"
},
{
"row_id":57,
"emp_number":"IPPH0004",
"timestamp":"11:33:09",
"attendance_status":"End Break",
"date_created":"2021-10-01"
}
]
到目前为止我做了什么:但是这种方法太慢了:
$timein = DB::table('attendance')->select('timestamp')->where('attendance_status','Punch In')->get();
现在我想要这样的东西:(PS这只是一个伪代码)
$timein = (where) $todaylog.attendance_status = "Punch In"
$endbreak = (where) $todaylog.attendance_status = "End Break"
这可能吗?或者我必须将它们单独查询到数据库?谢谢
可以使用laravel集合where方法:
collect($todaylog)->where("attendance_status", "Punch In")->first();
Laravel 你需要的是这样的东西:
$rows = collect([
[
"row_id" => 55,
"emp_number" => "IPPH0004",
"timestamp" => "03:30:23",
"attendance_status" => "Punch In",
"date_created" => "2021-10-01"
],
[
"row_id" => 56,
"emp_number" => "IPPH0004",
"timestamp" => "11:32:50",
"attendance_status" => "Start Break",
"date_created" => "2021-10-01"
],
[
"row_id" => 57,
"emp_number" => "IPPH0004",
"timestamp" => "11:33:09",
"attendance_status" => "End Break",
"date_created" => "2021-10-01"
]]);
dd($rows->where('row_id', 57));
结果:
Illuminate\Support\Collection {#446 ▼
#items: array:1 [▼
2 => array:5 [▼
"row_id" => 57
"emp_number" => "IPPH0004"
"timestamp" => "11:33:09"
"attendance_status" => "End Break"
"date_created" => "2021-10-01"
]
]
}