0

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??

3
  • It would be easier to provide a functional answer you provided an actual array with values for $positions. Commented Jun 3, 2020 at 22:41
  • stackoverflow.com/q/39415397/2943403 Commented Jun 3, 2020 at 23:32
  • @Bruno you should use Berto's technique during the pushing loop so that you can reduce the number of loops executed in your script. Commented Aug 10, 2020 at 22:49

3 Answers 3

1

Consider using a custom algorithm (this has a computational complexity of n, where using your way is 2n, so 2 times slower):

$positions= [...];
$min = $positions[0]['distance'];
$elements = [0];
foreach ($positions as $pos) {
   if ($pos["distance"] < $min) {
       $elements = [$pos];
       $min = $pos["distance"];
   } elseif ($pos["distance"] == $min) {
       $elements[] = $pos;
   }
}

But if you strictly need the ids, let me know and I will post the other algorithm.

Sign up to request clarification or add additional context in comments.

3 Comments

This will probably be the best performer on the page because it only needs to iterate the incoming data once compared to: array_column() (iteration), min() (iteration), then array_search() / array_keys() (iteration). On the other hand, if code brevity is more important or if you find the declarative syntax of functional programming more favorable, use John's suggestion.
Please not use else if (two words) in PHP -- it is a violation of PSR guidelines because it is rendered as an equivalent of } else { if (...) {}}` but is missing the recommended curly brace between the two control structures. php-fig.org/psr/psr-12
@mickmackusa wow, every day something new, thanks, edited
0

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);

Comments

0

You could also index by the id, sort ascending and get the first key:

$distances = array_column($positions, 'distance', 'id');
asort($distances);
$id = key($distances);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.