繁体   English   中英

在多维数组中查找最小值并返回索引号

[英]Find lowest value and return index number in multi dimensional array

我遍历查询结果时要构建一个多维数组,以便找到可以使用的所有可能部分。

$_SESSION['opt'][$Id] = array('rawStock'=>$materialStockID,'cost'=>$material_cost);

我需要能够找到成本最低的零件及其零件编号(将所有数据返回给生产线)。 我不反对使用其他方法来获得结果。 有没有更简单或更有效的方法。 数据在MySQL数据库中。

您可以通过以下简单查询获取它:

SELECT stockID, cost FROM table ORDER BY cost LIMIT 1

如果您不想执行其他查询,那么在遍历查询结果时可以找到最低价格:

$lowest_price = 99999999; // make this larger than any possible price
while ($row = fetch()) {
    // all your other code that processes the row here
    if ($material_cost < $lowest_price) {
        $lowest_price = $material_cost;
        $lowest_part = $materialStockID;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM