I have a loop to fill the $positions array like this:
array_push($positions, ["id" => $p->id, "distance"=> $distance, "date" => $p->date]);
Then i found the min key 'distance' like this:
$min = min(array_column($positions,'distance'));
Now i want to get the correspondent 'id' from the 'distance' founded.
How can i do this??
Untested:
$distances = array_column($positions,'distance', 'id');
$min = min($distances);
$id = array_search($min, $distances);
This gets the distances and creates a new array wth the id
values as the keys. Then it gets the minimum value and uses it to search for the key that corresponds to it.
I am unsure what the result will be if you have multiple items with the same minimum value so you should test that out. But if you want all of the IDs with the min value you can use array_keys()
with the optional search_value
parameter instead.
$ids = array_keys($distances, $min);