>您好,我怎样才能创建一个 php 函数,该函数的输入是一个数组,并且该函数找到数组中最小的数字。
此函数做了一些假设,例如传入的值实际上是一个包含一些值的数组。如果您愿意,您可以为此添加一些验证...
function findLowest($myArray)
{
$currLowest=$myArray[0];
foreach($myArray as $val)
{
if($val < $currLowest)
{
$currLowest=$val;
}
}
return $currLowest;
}
可以使用
内置函数或自定义函数来实现
$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.
}
}
使用函数中的任何一个逻辑。
$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.
}
}
这对我有帮助。:)