Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我能把变量从foreach循环中取出吗?并将其和响应以及所有用户数据一起发送_Php_Distance - Fatal编程技术网

Php 我能把变量从foreach循环中取出吗?并将其和响应以及所有用户数据一起发送

Php 我能把变量从foreach循环中取出吗?并将其和响应以及所有用户数据一起发送,php,distance,Php,Distance,我是否可以同时获得用户详细信息和距离以及用户详细信息 我需要在$data变量中获得的所有用户详细信息以及$distance 这两个变量都应该出现在响应上。 public function getProfessionalByCityLatLon(Request $request) { $data = DB::table('profiles') ->leftJoin('cities','profiles.city','=','cities.id')

我是否可以同时获得用户详细信息和距离以及用户详细信息

  • 我需要在$data变量中获得的所有用户详细信息以及$distance

  • 这两个变量都应该出现在响应上。

     public function getProfessionalByCityLatLon(Request $request)
         {
          $data = DB::table('profiles')
           ->leftJoin('cities','profiles.city','=','cities.id')
           ->leftJoin('users', 'profiles.user_id', '=', 'users.id')
           ->where('cities.name', '=', $request->select_city)                        
           ->get();
    
             foreach($data as $data1){
                 $longitude1 = 23.6676092; // i will get it later from $request 
                 $latitude1 = 86.0490814; // Same with this also
                 $longitude2 = $data1->longitude;
                 $latitude2 = $data1->latitude;
    
                 $unit = 'kilometers';
                 $theta = $longitude1 - $longitude2; 
                 $distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta))); 
                 $distance = acos($distance); 
                 $distance = rad2deg($distance); 
                 $distance = $distance * 60 * 1.1515; 
                 switch($unit) { 
                     case 'miles': 
                     break; 
                     case 'kilometers' : 
                     $distance = $distance * 1.609344;
                     $distance = $distance * 1000;  
    
                 } 
                 $profDistance = (round($distance, 2));
    
             }
             $response = [
                 'success' => true,
                 'distance' => $profDistance,
                 'user' => $data,
                 'message' => 'City retrieved successfully.',
             ];
    
             Log::info($response);
             return response()->json($response, 200);
         }
    

  • 我找到了解决方案,事实上,首先,我想要DB中的所有记录,其中城市名称和类别是根据参数确定的,然后我想要计算来自DB的每个记录的距离

    public function getProfessionalByCity($id, $currentLat, $currentLon)
        {
            Log::info('ye city select karne k baad on load calling ');
            // Log::info($id);
            $cityname = ucfirst($id);
                $newUser = DB::table('profiles')
                            ->leftJoin('cities','profiles.city','=','cities.id')
                            ->leftJoin('users', 'profiles.user_id', '=', 'users.id')
                            ->where('cities.name', '=', $cityname)
                            ->get();
                // Log::info($newUser);
                $longitude1 = $currentLat;
                $latitude1 = $currentLon;
                foreach($newUser as $user)
                {
                    $professionalAvatar = $user->avatar;
                    $profileId = $user->id;
                    $ProfessionalAvailable = $user->availability;
                    $ProfessionalName = $user->full_name;
                    $professionalProfession = $user->profession;
                    $latitude2 =  $user->latitude;
                    $longitude2 = $user->longitude;
                    $unit = 'kilometers';
                    $theta = $longitude1 - $longitude2; 
                    $distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta))); 
                    $distance = acos($distance); 
                    $distance = rad2deg($distance); 
                    $distance = $distance * 60 * 1.1515; 
                    switch($unit) { 
                        case 'miles': 
                        break; 
                        case 'kilometers' : 
                        $distance = $distance * 1.609344; 
                        // $distance = $distance * 1000;
                    }
                    $profId[] = $profileId ;
                    $calculatedDistance[] = (round($distance, 2));
                    $professName[] = $ProfessionalName;
                    $profession[] = $professionalProfession;
                    $availablity[] = $ProfessionalAvailable;
                    $avatar[] = $professionalAvatar;
                    // Log::info($calculatedDistance."ye h distance");
                    // return (round($distance,2)); 
                    
                }
                for($count = 0; $count < count($calculatedDistance); $count++)
                    {
                       $data = array(
                        'Distance' => $calculatedDistance[$count],
                        'ProfessionalName' => $professName[$count],
                        'Profession' => $profession[$count],
                        'ID' => $profId[$count],
                        'Availablity' => $availablity[$count], 
                        'Avatar' => $avatar[$count],
                        
                       );
                       $calDistance[] =$data;
                    } 
                $response = [
                    'success' => true,
                    'data' => $calDistance,
                    'message' => 'City retrieved successfully.',
                ];
            // Log::info($response);
            return response()->json($response, 200);
        }
    
    公共函数getProfessionalByCity($id、$currentLat、$currentLon)
    {
    日志::信息('ye city select karne k baad on load calling');
    //日志::信息($id);
    $cityname=ucfirst($id);
    $newUser=DB::表('profiles')
    ->leftJoin('cities','profiles.city','=','cities.id'))
    ->leftJoin('users','profiles.user_id','=','users.id')
    ->其中('cities.name','=',$cityname)
    ->get();
    //日志::信息($newUser);
    $longitude1=$currentLat;
    $latitude1=$currentLon;
    foreach($newUser作为$user)
    {
    $professionalAvatar=$user->avatar;
    $profileId=$user->id;
    $ProfessionalAvailable=$user->availability;
    $ProfessionalName=$user->full_name;
    $professionalprofessional=$user->professional;
    $latitude2=$user->latitude;
    $longitude2=$user->longitude;
    $单位='公里';
    $theta=$longitude1-$longitude2;
    $distance=(sin(deg2rad($latitude1))*sin(deg2rad($latitude2))+(cos(deg2rad($latitude1))*cos(deg2rad($latitude2))*cos(deg2rad($theta));
    $distance=acos($distance);
    $distance=rad2deg($distance);
    $distance=$distance*60*1.1515;
    开关(单位){
    “迈尔斯”一案:
    打破
    案例“公里”:
    $distance=$distance*1.609344;
    //$distance=$distance*1000;
    }
    $profId[]=$profileId;
    $calculatedDistance[]=(圆形($distance,2));
    $professName[]=$ProfessionalName;
    $professional[]=$professionalprofessional;
    $Availability[]=$Availability;
    $avatar[]=$avatar;
    //日志::信息($calculatedDistance.“Yeh距离”);
    //返回(圆形($distance,2));
    }
    对于($count=0;$count$calculatedDistance[$count],
    “ProfessionalName”=>$professName[$count],
    “专业”=>$Profession[$count],
    'ID'=>$profId[$count],
    “可用性”=>$availability[$count],
    “阿凡达”=>$Avatar[$count],
    );
    $calDistance[]=$data;
    } 
    $response=[
    “成功”=>正确,
    “数据”=>$calDistance,
    “已成功检索到消息'=>'城市”。”,
    ];
    //日志::信息($response);
    return response()->json($response,200);
    }
    
    这对我来说没什么意义。当数据库中有多条记录时,
    $proftrance
    中应该有什么?$proftrance用于计算距离。我们得到两个变量(来自请求的坐标)和两个来自DB的变量。在计算了它们之间的距离后,我希望结果应该与用户的其余信息一起出现在一个响应中。所以数据库中只有一条记录匹配?那么您根本不需要foreach的
    循环。我认为这是Laravel/雄辩的说法对吗?只需在查询中使用
    first()
    而不是
    get()
    。您将获得一条记录,然后可以使用它填充其余的数据。Thankyou@BenHillier我找到了解决方案,实际上,首先,我需要DB中的所有记录,其中城市名称和类别是根据参数确定的,然后我要计算来自DB的每条记录的距离。