Charanoglu
Charanoglu

Reputation: 1339

Cannot find min value in array to specific index

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.

Upvotes: 1

Views: 38

Answers (1)

Eddie
Eddie

Reputation: 26854

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

Upvotes: 2

Related Questions