Skip to content
Advertisement

Php find key for min value in 2D array

I have the following 2D array and I would like to get the key of the smalest value in the [0] column if done is equal to no

$graph= array(
"CityA" => array(
    "0" => "1",
    "1" => "CityC",
    "done" => "no",
    ),
"CityB" => array(
    "0" => "4",
    "1" => "CityA",
    "done" => "no",
    ),
"CityC" => array(
    "0" => "5",
    "1" => "CityA",
    "done" => "no",
    ),
);

Advertisement

Answer

Try this,

$arr = array_map(function($v){return $v[0];}, $graph);
$key = array_keys($arr, min($arr));
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement