1

I have an array that have this structure:

[
    {
        "id": "434073",
        "first_name": "Lucio Emanuel",
        "last_name": "Chiappero",
        "age": "24"
    },
    {
        "id": "330125",
        "first_name": "Luis Ezequiel",
        "last_name": "Unsain",
        "age": "23"
    }

my goal is search the player which have the minimum age, that in this case is 23, so Luis Ezequiel.

So I tried this code:

$index = array_search(min($players["age"]), $players);

the problem's that $index will return false, and this is really weird, because the array_search should find the index of the player which have min at the age key.

Charanoglu
  • 1,229
  • 2
  • 11
  • 30
  • Seems like duplicate of https://stackoverflow.com/questions/28372241/find-min-max-in-a-two-dimensional-array – mcyg Jun 30 '18 at 08:52
  • @MichalCygankiewicz if I do: `$index = min(array_column($players), 'age'); ` I get null – Charanoglu Jun 30 '18 at 08:54

1 Answers1

2

You can make the array into a simple array using array_column(). Use min() and array_search() to get the index.

$players = //Your array

$playersAge = array_column( $players, 'age' );             
$result = array_search(min($playersAge), $playersAge);

This will result to 1

Doc: array_column(), min(), array_search()

Eddie
  • 26,593
  • 6
  • 36
  • 58