dongmi1221 2014-07-03 21:14
浏览 59
已采纳

如何在给定范围内的多维数组中搜索最大值

I have a multidimensional $array that has a lot of various values. I am trying to get the maximum value in a given range in this case lets say the maximum value within x1,y0 and x2,y7, thus the result would be 6 using the data below.

It seems like a common problem for searching within coordinates in multidimensional array... but I cant find a simple solution and everything that I try to do seems to be rather extensive in the coding. I was wondering if there is a function that would allow me to search in this array using my criteria in an efficient way.

$array[0][0] = 1;
$array[0][1] = 10;
$array[0][3] = 3;
$array[1[0] = 1;
$array[1][1] = 1;
$array[2][5] = 6;
  • 写回答

3条回答 默认 最新

  • doujing9972 2014-07-03 21:20
    关注
    $array[0][0] = 1;
    $array[0][1] = 10;
    $array[0][3] = 3;
    $array[1][0] = 1;
    $array[1][1] = 1;
    $array[2][5] = 6;
    
    $max = false;
    $x1 = 1; $x2 = 2;
    $y1 = 0; $y2 = 7;
    
    foreach($array as $x => $y_vals) {    
        if($x < $x1 || $x > $x2)
            continue;
    
        foreach($y_vals as $y => $val) {
            if($y < $y1 || $y > $y2)
                continue;
    
            if(false === $max || $max < $val)
                $max = $val;
        }
    }
    
    print $max; //6
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题