php取最小值
<?php
//连接数据库
$myconn=mysql_connect("localhost","root","");
mysql_select_db("nowamagic",$myconn);
$strSql="
select article_ID
from article
where article_ID=(select max(article_ID)
from article)
";
$strSql2="
select article_ID
from article
where article_ID=(select min(article_ID)
from article)
";
$result=mysql_query($strSql,$myconn) or die(mysql_error());
$result2=mysql_query($strSql2,$myconn) or die(mysql_error());
$row_max=mysql_fetch_array($result);
$row_min=mysql_fetch_array($result2);
// 输出ID最大值
//echo $row_max["article_ID"];
//echo "
";
// 输出ID最小值
//echo $row_min["article_ID"];
$article_count = $row_max["article_ID"] - $row_min["article_ID"];
?>
② PHP中如何使用foreach循环遍历数组求6、8、10、4、3中的最大值和最小值
不需要使用
foreach
就能从数组
6、8、10、4、3
求出最大最小值,可以使用以下代码:
12345678<?php $a = array(8,40,3,5,6,10);sort($a, SORT_NUMERIC);//最小值$min = reset($a);//最大值$max = end($a);
所需知识点:
数组排序函数
sort
数组游标
③ 在一个php数组中,里面有(78.65.28.35等)求最大值和最小值,(不能用max等函数)
扫描一遍就可以获得最大值、最小值、平均值,扫描数组使用foreach,下面是例子代码:
<?php
$arr=array(78,65,28,35);
$max=$arr[0];
$min=$arr[0];
$sum=0;
$num=0;
foreach($arras$x){
if($x>$max)$max=$x;
if($x<$min)$min=$x;
$sum+=$x;
$num++;
}
$avg=$sum/$num;
echo"最大值{$max},最小值{$min},平均值{$avg}";
?>
④ 用php程序求数组{98,79,81,60,70}的最大最小值,要求不能调用函数,请问要怎么写
$arr=array(98,79,81,60,70);
$max=$arr[0];
$min=$arr[0];
array_walk($arr,'arr_min');
array_walk($arr,'arr_max');
function arr_min($v,$k){
global $min;
if($min>$v){
$min=$v;
}
}
function arr_max($v,$k){
global $max;
if($max<$v){
$max=$v;
}
}
echo $max."<Br/>"; //输出最大值
echo $min."<Br/>"; //输出最小值
⑤ PHP里获取一维数组里的最大值和最小值要求,效率最好,速度最快
还有什么会比PHP的方法高效?
最大的
<?php
$a=array('1','3','55','99');
$pos=array_search(max($a),$a);
echo$a[$pos];
最小的
<?php
$a=array('1','3','55','99');
$pos=array_search(min($a),$a);
echo$a[$pos];
⑥ PHP如何获取数据库中的最大最小值并且区分十个区间
<?php
//连接数据库
$myconn=mysql_connect("localhost","root","");
mysql_select_db("nowamagic",$myconn);
$strSql="
select article_ID
from article
where article_ID=(select max(article_ID)
from article)
";
$strSql2="
select article_ID
from article
where article_ID=(select min(article_ID)
from article)
";
$result=mysql_query($strSql,$myconn) or die(mysql_error());
$result2=mysql_query($strSql2,$myconn) or die(mysql_error());
$row_max=mysql_fetch_array($result);
$row_min=mysql_fetch_array($result2);
// 输出ID最大值
//echo $row_max["article_ID"];
//echo "
";
// 输出ID最小值
//echo $row_min["article_ID"];
$article_count = $row_max["article_ID"] - $row_min["article_ID"];
?>
⑦ php如何用数组对mysql表中某字段内的所有数值取最大值或最小值
PHP有SORT函数;
$arr=array('26','3',); //数组
sort($arr,SORT_NUMERIC); //按数字升序排序
⑧ PHP判断多个数字(含小数)大小,并取出最小值,程序效率高的追加50分!
function tomax($str)
{
if ($str=="")
return "字符串不能为空"; //返回err1,表示str为空
else {
$arr=explode("/",$str); //以"/"字符分割字符串
$max=$arr[0]; //记录最新数变量
for($i=0;$i<count($arr);$i++)
{
if($arr[$i]=="")
; //假如为空字符,空语句
else {
if($max!="")
{
if($arr[$i]<$max) //比较数组的值
$max=$arr[$i];
}
else
$max=$arr[$i];
}
}
if($max=="")
return "无法比较"; //当全为空字符的返回值
else
return $max;
}
}
⑨ PHP数组,怎么找出最大值与最小值,和它们所对应的数组下标
<?php
$hots=array('8213'=>0,'8212'=>100,'8172'=>10008);
$maxkey=array_search(max($hots),$hots);
$minkey=array_search(min($hots),$hots);
echo"最大值数组所在下标:".$maxkey;
echo"<br>";
echo"最小值数组所在下标:".$minkey;
?>
⑩ php取得数组最小值
PHP获取数组最小值使用内置函数:min()