你好我怎么能创建一个php函数,输入是一个数组,函数找到数组中最小的数字。
答案 0 :(得分:1)
这个函数做了一些假设,比如传入的值实际上是一个包含一些值的数组。如果您愿意,可以为此添加一些验证...
function findLowest($myArray)
{
$currLowest=$myArray[0];
foreach($myArray as $val)
{
if($val < $currLowest)
{
$currLowest=$val;
}
}
return $currLowest;
}
答案 1 :(得分:0)
可以使用内置函数或自定义函数来实现
$numbers=array( 4 => 40, 3 => 30, 13 => 38 );
function getMin($source = array())
{
//If you don't need your original index for the lowest value
sort($source);
// after sort original index is lost.
return $source[0]
//OR
//If you need your original index for the lowest value
asort($source);
foreach($source as $key => $value)
{
return $value; //ie return key or value that you need.
}
}
使用函数中的任何一个逻辑。
答案 2 :(得分:0)
$numbers=array( 4 => 40, 3 => 30, 13 => 38 );
function getMin($source = array())
{
//If you don't need your original index for the lowest value
sort($source);
// after sort original index is lost.
return $source[0]
//OR
//If you need your original index for the lowest value
asort($source);
foreach($source as $key => $value)
{
return $value; //ie return key or value that you need.
}
}
这对我有帮助。 :)