本文介绍了多维数组的最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的阵列
阵列
(
[2014年8月14日] =>排列
(
[18:00:00] =>排列
(
[6] =>排列
(
[价格] => 15.36
[avail_clean] =>五
[avail_noclean] => 6
) [7] =>排列
(
[价格] => 17.35
[avail_clean] => 2
[avail_noclean] => 3
) ) [19:00:00] =>排列
(
[6] =>排列
(
[价格] => 15.36
[avail_clean] =>五
[avail_noclean] => 6
) [7] =>排列
(
[价格] => 17.35
[avail_clean] => 2
[avail_noclean] => 3
) ) ))
我怎样才能为6安培以下; 7分开:
- 价格的总和
- avail_clean的最大值
- 的最大avail_noclean
我得到的远远的价格:
的foreach($预订为$日期=> $键){
的foreach($键$时间=> $ KEY2){
的foreach($ KEY2为$房=> $ KEY3){
的foreach($ KEY3为$价格=> $ KEY4){
如果($价格==价格){
如果(使用isset($ sumRoom [$室] ['总'])){
$ sumRoom [$室] ['总'] + = $ KEY4;
}其他{
$ sumRoom [$室] ['总'] = $ KEY4;
}
}
}
} }
}
给我这个
阵列(
[6] =>排列
(
[总] => 30,72
)[7] =>排列
(
[总] => 34,7
)
)
但有关MAX(),我应该在哪里放什么?
解决方案
的foreach($预订为$日期=> $次){
的foreach($倍$时间=> $房间){
的foreach($房间为$房=> $选项){$ sumRoom [$室] ['总'] = 0;
的foreach($选项,$选项=> $值){ 如果($选项==价格){
$ sumRoom [$室] ['总'] + = $价值;
}
如果($选项==avail_clean){
$ avail_clean [$室] [] = $价值;
}
如果($选项==avail_noclean){
$ avail_noclean [$室] [] = $价值;
} }
$ sumRoom [$室] ['avail_clean_max'] = MAX($ avail_clean [$室]);
$ sumRoom [$室] ['avail_noclean'] = MAX($ avail_noclean [$室]);
}
}
}
我已经编辑我的答案。我已经合并在同一个阵列即 $ sumRoom
I have this Array
Array
(
[2014-08-14] => Array
(
[18:00:00] => Array
(
[6] => Array
(
[price] => 15.36
[avail_clean] => 5
[avail_noclean] => 6
)
[7] => Array
(
[price] => 17.35
[avail_clean] => 2
[avail_noclean] => 3
)
)
[19:00:00] => Array
(
[6] => Array
(
[price] => 15.36
[avail_clean] => 5
[avail_noclean] => 6
)
[7] => Array
(
[price] => 17.35
[avail_clean] => 2
[avail_noclean] => 3
)
)
)
)
How can I get the following for 6 & 7 seperately:- Sum of price- max of avail_clean- max of avail_noclean
I got that far for the price:
foreach ($bookable as $date=>$key) {
foreach ($key as $time=>$key2) {
foreach($key2 as $room=>$key3){
foreach($key3 as $price=>$key4){
if($price == "price"){
if(isset($sumRoom[$room]['total'])){
$sumRoom[$room]['total'] += $key4;
}else{
$sumRoom[$room]['total'] = $key4;
}
}
}
}
}
}
Gives me this
Array(
[6] => Array
(
[total] => 30,72
)
[7] => Array
(
[total] => 34,7
)
)
But what about the max(), where should I put that?
解决方案
foreach ($bookable as $date=>$times) {
foreach ($times as $time=>$rooms) {
foreach($rooms as $room=>$options){ $sumRoom[$room]['total'] = 0;
foreach($options as $option=>$value){
if($option == "price"){
$sumRoom[$room]['total'] += $value;
}
if($option == "avail_clean"){
$avail_clean[$room][] = $value;
}
if($option == "avail_noclean"){
$avail_noclean[$room][] = $value;
}
}
$sumRoom[$room]['avail_clean_max'] = max($avail_clean[$room]);
$sumRoom[$room]['avail_noclean'] = max($avail_noclean[$room]);
}
}
}
I've edited my answer.. I've merged all in one array i.e. $sumRoom
这篇关于多维数组的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!