'Clean an array in Laravel
I have a function that retrieves an array of values from a DB with a query in Laravel. I need to clean that array
From this: 2022-02-21 07:40:16
To this: 07:40:16
How can I do?
The function is this:
public function singleDev(Device $deviceID)
{
$data = DataFromRasp::where('MAC', 'C4:A5:DF:24:05:7E')->get()->pluck('RSSI', 'created_at')->toArray();
$time_array = [];
$rssi_array = [];
$cnt = 0;
foreach ($data as $key => $value) {
array_push($time_array, $key);
array_push($rssi_array, $value);
if ($value < -60) {
$cnt++;
if ($cnt >= 2) {
}
}
}
dd($time_array);
return view('backend.auth.user.singleDevice', compact("time_array", "rssi_array"));
}
Solution 1:[1]
You can use the power of Laravel Collection methods like map, pluck, and make the solution short.
$data = DataFromRasp::select('RSSI', 'created_at')->where('MAC', 'C4:A5:DF:24:05:7E')->map(function($d) {
$d->timeValue = $d->created_at->format('H:i:s');
return $d;
});
$time_array = $data->pluck('timeValue')->toArray();
$rssi_array = $data->pluck('RSSI')->toArray();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
