在 Laravel 中清理数组

问题描述 投票:0回答:2

我有一个函数,可以通过 Laravel 中的查询从数据库中检索值数组。我需要清理那个阵列

由此: 2022-02-21 07:40:16

对此: 07:40:16

我该怎么办?

功能是这样的:

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"));
}
php arrays laravel
2个回答
0
投票

试试这个

public function singleDev(Device $deviceID){
        $data = DataFromRasp::select('RSSI', 'created_at')->where('MAC', 'C4:A5:DF:24:05:7E')->get();
        $time_array = [];
        $rssi_array = [];
        $cnt = 0;
        foreach($data as $dataItem){
            array_push($time_array, $dataItem->created_at->format('H:i:s'));
            array_push($rssi_array, $dataItem->RSSI);
            if($value < -60){
                $cnt++;
                if($cnt >=2){

                }
            }
        }
        dd($time_array);

        return view('backend.auth.user.singleDevice', compact("time_array", "rssi_array"));
    }

0
投票

您可以使用 Laravel Collection 方法的强大功能,例如 map、pluck,并使解决方案变得简短。

$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();
© www.soinside.com 2019 - 2024. All rights reserved.