如何返回数组中的最小键?

时间:2021-12-10 06:53:32

Is there an equivalent min() for the keys in an array?

数组中的键是否有等效的min()?

Given the array:

鉴于阵列:

$arr = array(300 => 'foo', 200 => 'bar');

How can I return the minimum key (200)?

如何返回最小键(200)?

Here's one approach, but I have to imagine there's an easier way.

这是一种方法,但我必须想象有一种更简单的方法。

function minKey($arr) {
    $minKey = key($arr);
    foreach ($arr as $k => $v) {
        if ($k < $minKey) $minKey = $k;
    }
    return $minKey;
}
$arr = array(300 => 'foo', 200 => 'bar');
echo minKey($arr); // 200

5 个解决方案

#1


23  

Try this:

尝试这个:

echo min(array_keys($arr));

#2


7  

Try with

试试吧

echo min(array_keys($arr));

min() is a php function that will return the lowest value of a set. array_keys() is a function that will return all keys of an array. Combine them to obtain what you want.

min()是一个php函数,它将返回一个集合的最低值。 array_keys()是一个返回数组所有键的函数。结合它们来获得你想要的东西。

If you want to learn more about this two functions, please take a look to min() php guide and array_keys() php guide

如果您想了解有关这两个函数的更多信息,请查看min()php guide和array_keys()php guide

#3


1  

use array_search() php function.

使用array_search()php函数。

array_search(min($arr), $arr);

above code will print 200 when you echo it.

回复时,上面的代码将打印200。

For echoing the value of lowest key use below code,

为了回显代码下面最低密钥使用的值,

echo $arr[array_search(min($arr), $arr)];

Live Demo

现场演示

#4


0  

$arr = array(
300 => 'foo', 200 => 'bar'
);
$arr2=array_search($arr , min($arr ));
echo $arr2;

#5


0  

This also would be helpful for others,

这对其他人也有帮助,

<?php
//$arr = array(300 => 'foo', 200 => 'bar');
$arr = array("0"=>array('price'=>100),"1"=>array('price'=>50));

//here price = column name
echo minOfKey($arr, 'price');


function minOfKey($array, $key) {
    if (!is_array($array) || count($array) == 0) return false;
    $min = $array[0][$key];
    foreach($array as $a) {
        if($a[$key] < $min) {
            $min = $a[$key];
        }
    }
    return $min;
}

?>

#1


23  

Try this:

尝试这个:

echo min(array_keys($arr));

#2


7  

Try with

试试吧

echo min(array_keys($arr));

min() is a php function that will return the lowest value of a set. array_keys() is a function that will return all keys of an array. Combine them to obtain what you want.

min()是一个php函数,它将返回一个集合的最低值。 array_keys()是一个返回数组所有键的函数。结合它们来获得你想要的东西。

If you want to learn more about this two functions, please take a look to min() php guide and array_keys() php guide

如果您想了解有关这两个函数的更多信息,请查看min()php guide和array_keys()php guide

#3


1  

use array_search() php function.

使用array_search()php函数。

array_search(min($arr), $arr);

above code will print 200 when you echo it.

回复时,上面的代码将打印200。

For echoing the value of lowest key use below code,

为了回显代码下面最低密钥使用的值,

echo $arr[array_search(min($arr), $arr)];

Live Demo

现场演示

#4


0  

$arr = array(
300 => 'foo', 200 => 'bar'
);
$arr2=array_search($arr , min($arr ));
echo $arr2;

#5


0  

This also would be helpful for others,

这对其他人也有帮助,

<?php
//$arr = array(300 => 'foo', 200 => 'bar');
$arr = array("0"=>array('price'=>100),"1"=>array('price'=>50));

//here price = column name
echo minOfKey($arr, 'price');


function minOfKey($array, $key) {
    if (!is_array($array) || count($array) == 0) return false;
    $min = $array[0][$key];
    foreach($array as $a) {
        if($a[$key] < $min) {
            $min = $a[$key];
        }
    }
    return $min;
}

?>