如何在多维数组中获取最大值和最小值

时间:2018-11-27 13:14:55

标签: php arrays multidimensional-array

我有这个数组:

array(8) {
  [0]=>
  array(2) {
    ["ticket_type"]=>
    string(9) "normal"
    ["ticket_price"]=>
    string(5) "82.00"
  }
  [1]=>
  array(2) {
    ["ticket_type"]=>
    string(9) "cheaper"
    ["ticket_price"]=>
    string(5) "62.00"
  }
  [2]=>
  array(2) {
    ["ticket_type"]=>
    string(9) "normal"
    ["ticket_price"]=>
    string(6) "182.00"
  }
  [3]=>
  array(2) {
    ["ticket_type"]=>
    string(9) "cheaper"
    ["ticket_price"]=>
    string(6) "162.00"
  }
  [4]=>
  array(2) {
    ["ticket_type"]=>
    string(9) "normal"
    ["ticket_price"]=>
    string(6) "103.00"
  }
  [5]=>
  array(2) {
    ["ticket_type"]=>
    string(9) "cheaper"
    ["ticket_price"]=>
    string(5) "63.00"
  }
  [6]=>
  array(2) {
    ["ticket_type"]=>
    string(9) "normal"
    ["ticket_price"]=>
    string(6) "203.00"
  }
  [7]=>
  array(2) {
    ["ticket_type"]=>
    string(9) "cheaper"
    ["ticket_price"]=>
    string(6) "163.00"
  }
}

我想获取“正常”和“便宜”机票类别的最低和最高价格,会有更多的机票类别,所以不能对其进行硬编码,可以从DB获取,我该怎么做?我现在正在使用PHP 5.6,需要导出为数组或json,如果需要更多详细信息,请告诉我。

谢谢

3 个答案:

答案 0 :(得分:1)

您可以在SQL级别上做到这一点:

SELECT MIN(ticket_price) AS min_ticket_price, MAX(ticket_price) as max_ticket_price, ticket_type 
FROM {your_table_name} 
WHERE {your_conditions} 
GROUP BY ticket_type

答案 1 :(得分:0)

如果您仍想使用PHP进行操作,则可以循环数组并对票证类型进行排序,然后获取最小值和最大值。

foreach($arr as $ticket){
    $new[$ticket["ticket_type"]][] = $ticket["ticket_price"];
}

// loop the new array to only get the max and min values of each ticket type
foreach($new as $key => $n){
    $res[$key]["max"] = max($n);
    $res[$key]["min"] = min($n);
}

输出:

array(2) {
  ["normal"]=>
  array(2) {
    ["max"]=>
    string(6) "203.00"
    ["min"]=>
    string(5) "82.00"
  }
  ["cheaper"]=>
  array(2) {
    ["max"]=>
    string(6) "163.00"
    ["min"]=>
    string(5) "62.00"
  }
}

https://3v4l.org/RcCHZ

答案 2 :(得分:0)

这是在php级别上要做的解决方案:

$prices = [ 
  ["ticket_type"=>"normal", "ticket_price"=>"82.00"],
  ["ticket_type"=>"cheaper", "ticket_price"=>"62.00"],
  ["ticket_type"=>"normal", "ticket_price"=>"182.00"],
  ["ticket_type"=>"cheaper", "ticket_price"=>"162.00"],
  ["ticket_type"=>"normal", "ticket_price"=>"103.00"],
  ["ticket_type"=>"cheaper", "ticket_price"=>"63.00"],
  ["ticket_type"=>"normal", "ticket_price"=>"203.00"],
  ["ticket_type"=>"cheaper", "ticket_price"=>"163.00"],
];

// STEP:1 group by ticket types
$grouped_array = [];
foreach($prices as $price) {
   $ticket_type = $price["ticket_type"];
   $grouped_array[$ticket_type][] = $price["ticket_price"];
}

/* 
$grouped_array = [
 "normal" => ["82.00", "182.00", "103.00", "203.00"],
 "cheaper => ["62.00", "162.00", "63.00", "163.00"]
];
*/


function minMax($type_array) {
  $min_max = ['min'=> min($type_array), 'max'=>max($type_array)];
  return $min_max;
}

// STEP 2: find min and max in each category
$min_max = [];
foreach($grouped_array as $type => $prices) {
  $min_max[$type] = minMax($prices);
}

/* 
$min_max = [
 "normal" => ["min"=>"82.00", "max"=>"203.00"],
 "cheaper => ["min"=>"62.00", "max"=>"163.00"]
];
*/

但是,在您的情况下,最好在数据库层上执行此操作。这回答了“如何在多维数组中获得最大值和最小值”。