-1

I have an multidimensional array like this :

$array = 
[
    ['groupe' => 1510, 'nombre' => 3],
    ['groupe' => 1511, 'nombre' => 10],
    ['groupe' => 1512, 'nombre' => 5],
    ['groupe' => 1513, 'nombre' => 4],
    ['groupe' => 1514, 'nombre' => 3]
];

I want to find the min value for 'nombre' key and return its array. If many arrays have the same min value, I want to return only the first one.

How can I do this ? I found how to get the min value but I can't find how to return its array and only the first found.

To get min value :

$min = min(array_column($array, 'nombre'));
Kristen Joseph-Delaffon
  • 1,261
  • 2
  • 17
  • 37

1 Answers1

0
$array = [
    ['groupe' => 1510, 'nombre' => 3],
    ['groupe' => 1511, 'nombre' => 10],
    ['groupe' => 1512, 'nombre' => 5],
    ['groupe' => 1513, 'nombre' => 4],
    ['groupe' => 1514, 'nombre' => 3],
];

$min = null;
foreach ($array as $a) {
    if ($min === null) {
        $min = $a;
    } elseif ($min['nombre'] > $a['nombre']) {
        $min = $a;
    }
}

// $min now contains what your are looking for.
colburton
  • 4,685
  • 2
  • 26
  • 39