Home > front end >  Need to push dynamic values in array with foreach loop php
Need to push dynamic values in array with foreach loop php

Time:12-24

I'm working on a project and got stuck on a minor issue. Here is the scenario

I have a function that takes some values from a database and performs some calculations on them before storing them in variables. The problem now is that I must repeat it in a loop, i.e., the number of users in the database, and the loop will be executed. The problem I'm facing is how I can store those values in an array and send the array to "view," where I can print those values.

Controller function

   public function FindNearByPeople(Request $request)
    {
        $user_id = Auth::user()->id;
        $long_current_trip = //fetch from db;
        $lat_current_trip = //fetch from db;
        $trip_country = //fetch from db;
        $All_Trips_Data = //fetch from db;

Here is I have defined the array. You can suggest me the better way i can handle it

        $peopleData_Nearby = array(
            'user_id' => array(),
            'trip_id' => array(),
            'first_name' => array(),
            'last_name' => array(),
            'distance' => array()
        );

This loop will be executed x no of time depending on db result

        foreach ($All_Trips_Data as $all_trip) {
            $user_id = $all_trip->user_id;
            $trip_ID = $all_trip->id;
            $trip_lat =  $all_trip->trip_latitude;
            $trip_long = $all_trip->trip_longitude;

            $user_first_name = //fetch first name from db;

            $user_last_name = //fetch last name from db;

            $distance = $this->CalculateDistance($lat_current_trip, $long_current_trip, $trip_lat, $trip_long);

            $user_full_name = $user_first_name." ".$user_last_name;

            $peopleData_Nearby = array(
                "user_id"=>$user_id,
                "user_name"=>$user_full_name,
                "trip_id"=>$trip_ID,
                "distance"=>$distance
            );
        }

Problem is this loop should display the data of each user but it is displaying for first user only

        foreach($peopleData_Nearby as $key => $value){
            echo "<p>$key: $value <p>";

        }
    
    }

I have tried to solve it through key value pair but problem with that is when displaying it on view it will display all the username first then user id and so on but i want to display like this:

UserName : David Distance : 2KM UserID : 1

UserName : Mike Distance : 5KM UserID : 2

CodePudding user response:

Change this:

     $peopleData_Nearby = array(
            "user_id"=>$user_id,
            "user_name"=>$user_full_name,
            "trip_id"=>$trip_ID,
            "distance"=>$distance
     );

To this:

   $peopleData_Nearby[] = array(
            "user_id"=>$user_id,
            "user_name"=>$user_full_name,
            "trip_id"=>$trip_ID,
            "distance"=>$distance
     );

This will push all the data to the array

You can loop over $peopleData_Nearby

     foreach($peopleData_Nearby as $person){
        echo "UserName :" . $person['user_name'] . "Distance :" . $person['distance'] . "UserID :" . $person['user_id'];

    }
  • Related