取出数组中最大值解决方案

WBOY
Release: 2016-06-13 10:20:14
Original
923 people have browsed it

取出数组中最大值
$t=array('1','2','3','4','5');
$result=array_search(max($t),$t);
echo '最大值:'.$result;
  ?>
这个结果为什么显示是4
吐血了

------解决方案--------------------
max($t)是5 
$result=array_search(5,$t);
这是在$t数组中寻找值为5的元素 返回这个元素对应的键。
5存在于这个数组 并且键是4 所以返回4是ok的
数组的键从0开始


你的程序最后一句话改为 echo '最大值的键:'.$result;
------解决方案--------------------
echo '最大值:'. max($t);

何必要画蛇添足的 array_search 呢?

------解决方案--------------------

探讨
$t=array('1','2','3','4','5');
$result=array_search(max($t),$t);
echo '最大值:'.$result;
?>
这个结果为什么显示是4
吐血了
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!