PHP如何取二维数组中的某列的最大值和最小值?
5个回答
展开全部
需要准备的材料分别是:电脑、php编辑器、浏览器。
1、首先宴培羡,打开php编辑器,新建晌拍php文件,例如:index.php,中桐定义一个二维数组,以取第2列的最值为例。
2、在index.php中,输入代码:
$a = [
[11, 5, 24, 8, 3],
[3, 54, 78, 64, 9],
];
echo 'max=' . max($a[1]) . '<br/>';
echo 'min=' . min($a[1]);
3、浏览器运行index.php页面,此时发现第2列的最大值和最小值都被取出了。
展开全部
<?
$a = array(
array(3,1,2,5,4),
array(7,8,10,9,6),
array(15,12,14,13,11),
);
$b = $a;
for($i=0; $i<count($b); $i++){
sort($b[$i]);
echo '第'.$i.'列带旁陆 最小数='.$b[$i][0].' 最大数='.$b[$i][count($b[$i])-1].'<蠢顷br>启唯';
}
?>
$a = array(
array(3,1,2,5,4),
array(7,8,10,9,6),
array(15,12,14,13,11),
);
$b = $a;
for($i=0; $i<count($b); $i++){
sort($b[$i]);
echo '第'.$i.'列带旁陆 最小数='.$b[$i][0].' 最大数='.$b[$i][count($b[$i])-1].'<蠢顷br>启唯';
}
?>
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
$topic_wrong = array(4) {
[0] => array(4) {
["id"] => string(2) "18"
["topic_id"] => string(3) "219"
["knowpoint_time"答塌] => string(1) "0"
["moexam_time"] => string(1) "0"
}
[1] => array(4) {
["id"] => string(2) "19"
["清灶圆topic_id"] => string(3) "222"
["knowpoint_time"] => string(1) "0"
["moexam_time"] => string(1) "0"
}
[2] => array(4) {
["id"] => string(2) "20"
["topic_id"] => string(3) "224"
["knowpoint_time"] =>辩掘 string(1) "0"
["moexam_time"] => string(1) "0"
}
[3] => array(4) {
["id"] => string(2) "21"
["topic_id"] => string(3) "225"
["knowpoint_time"] => string(1) "0"
["moexam_time"] => string(1) "0"
}
}
//将数组根据topic_id逆序
array_multisort(array_column($topic_wrong,'topic_id'),SORT_DESC,$topic_wrong);
$max = $topic_wrong[0]['topic_id']
[0] => array(4) {
["id"] => string(2) "18"
["topic_id"] => string(3) "219"
["knowpoint_time"答塌] => string(1) "0"
["moexam_time"] => string(1) "0"
}
[1] => array(4) {
["id"] => string(2) "19"
["清灶圆topic_id"] => string(3) "222"
["knowpoint_time"] => string(1) "0"
["moexam_time"] => string(1) "0"
}
[2] => array(4) {
["id"] => string(2) "20"
["topic_id"] => string(3) "224"
["knowpoint_time"] =>辩掘 string(1) "0"
["moexam_time"] => string(1) "0"
}
[3] => array(4) {
["id"] => string(2) "21"
["topic_id"] => string(3) "225"
["knowpoint_time"] => string(1) "0"
["moexam_time"] => string(1) "0"
}
}
//将数组根据topic_id逆序
array_multisort(array_column($topic_wrong,'topic_id'),SORT_DESC,$topic_wrong);
$max = $topic_wrong[0]['topic_id']
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<猜裤?
$arr = array(
array(3,1,2,5,4),
array(7,8,10,9,6),
array(15,12,14,13,11),
);
$b = array();
foreach($arr as $key=>$value){
$a=$arr[$key];
$b = array_merge($a,$b);
sort($b);
}
echo $b[0]."<p>穗缓简"哪虚.$b[count($b)-1];
$arr = array(
array(3,1,2,5,4),
array(7,8,10,9,6),
array(15,12,14,13,11),
);
$b = array();
foreach($arr as $key=>$value){
$a=$arr[$key];
$b = array_merge($a,$b);
sort($b);
}
echo $b[0]."<p>穗缓简"哪虚.$b[count($b)-1];
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2018-04-30
展开全部
<?php
function arrMax($arr,$key,$v=true){
$s = '';
foreach($arr as $k){
$s.= $k[$key].',';
}
$s = substr($s,0,-1);
$v = $v===false?'min':'max';
eval('$v = '.$v.'('.$s.');');
return $v;
}//求二维数组最大值或最小值
$arr = array(
'9'=>array(
'name'=>'张三',
'age'=>'30'
),
'6'=>array(
'name'=>'李四',
'age'=>'40'
吵氏 ),
'title'=>array(
'name'=>'王二',
慧碰段 'age'=>'10'
前誉 )
);
$max = arrMax($arr,'age');
$min = arrMax($arr,'age',false);
var_dump($max);
var_dump($min);
?>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |