nutkani1337's avatar

Need to push dynamic values in array with foreach loop php

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

0 likes
2 replies
vincent15000's avatar
Level 63

That's normal because in your loop, you reassign each value of the same item in the array.

 foreach ($All_Trips_Data as $all_trip) {
	....
    $peopleData_Nearby = array(
        "user_id"=>$user_id,
        "user_name"=>$user_full_name,
        "trip_id"=>$trip_ID,
        "distance"=>$distance
    );
}

You should add the values in the array like this.

$peopleData_Nearby = [];

 foreach ($All_Trips_Data as $all_trip) {
	....
    $peopleData_Nearby[] = [
        "user_id"=>$user_id,
        "user_name"=>$user_full_name,
        "trip_id"=>$trip_ID,
        "distance"=>$distance
    ];
}
1 like
Tray2's avatar

You really should do that calculation inside the database

2 likes

Please or to participate in this conversation.