我们如何将数据推送到laravel5中的数组?

时间:2019-04-22 10:34:18

标签: angularjs laravel

我们如何将数据推送到laravel5中的数组?

将数据推送到数组:-

下面有一些我共享的代码:

if(!empty($request))
{
    $request->all();
    $lan_user_location = $request[0];
    $lat_user_location = $request[1];

    $vehiclelocation = Vehicle::select('veh_last_location')->get()->toArray();
    $location = json_decode($vehiclelocation[849]['veh_last_location']);
    $rows[] = $location;

    foreach($rows as $key=>$value){
        $lan_vehicle=$rows[$key][$key]->long;
        $lat_vehicle=$rows[$key][$key]->lati;
    }

    $location = json_decode($vehiclelocation[851]['veh_last_location']);
    $distance = $this->nearesrtdistance(
                        $lat_user_location,
                        $lan_user_location,
                        $lat_vehicle, 
                        $lan_vehicle
                );

    if($distance<30)
    {
        $nearlatlong[] = $lat_vehicle;
        $nearlatlong[] = $lan_vehicle;

        $response = [
            'status'        => 'success', 
            'nearlatlong'   => $nearlatlong, 
            'statusCode'    => Response::HTTP_OK
        ];

        return response()->json(['response' => $response]);
}

想在$ nearlatlong数组上推lat并加长

2 个答案:

答案 0 :(得分:0)

尝试一下:

$nearlatlong[] = array(
       'lat' => $lat_vehicle,
       'lan' => $lan_vehicle
 );

要从$nearlatlong取数据

foreach($nearlatlong as $nearlatlong){
    $lat_vehicle = $nearlatlong->lat;
    $lan_vehicle = $nearlatlong->lan;
}

通过这种方式,您可以将多个lan&lat存储到单个阵列中。

答案 1 :(得分:-1)

您可以使用array_push()函数将项目推入php数组。 array_push()接收数组和要添加为参数的所有项目。

例如:

<?php
 $array1= array('Mathematics','Physics');
 array_push($array1,'Chemistry','Biology');
 print_r($array1);
?>