这是一个示例数组。我试图获得两个循环的值,例如,此数组将返回键21和143作为最低值,80和204作为最高值。
Array ( [0] => Array ( [t] => 2016-08-31 00:00 [v] => 1.443 ) [1] => Array ( [t] => 2016-08-31 00:06 [v] => 1.319 ) [2] => Array ( [t] => 2016-08-31 00:12 [v] => 1.199 ) [3] => Array ( [t] => 2016-08-31 00:18 [v] => 1.082 ) [4] => Array ( [t] => 2016-08-31 00:24 [v] => 0.969 ) [5] => Array ( [t] => 2016-08-31 00:30 [v] => 0.861 ) [6] => Array ( [t] => 2016-08-31 00:36 [v] => 0.757 ) [7] => Array ( [t] => 2016-08-31 00:42 [v] => 0.658 ) [8] => Array ( [t] => 2016-08-31 00:48 [v] => 0.565 ) [9] => Array ( [t] => 2016-08-31 00:54 [v] => 0.477 ) [10] => Array ( [t] => 2016-08-31 01:00 [v] => 0.395 ) [11] => Array ( [t] => 2016-08-31 01:06 [v] => 0.320 ) [12] => Array ( [t] => 2016-08-31 01:12 [v] => 0.251 ) [13] => Array ( [t] => 2016-08-31 01:18 [v] => 0.190 ) [14] => Array ( [t] => 2016-08-31 01:24 [v] => 0.135 ) [15] => Array ( [t] => 2016-08-31 01:30 [v] => 0.088 ) [16] => Array ( [t] => 2016-08-31 01:36 [v] => 0.049 ) [17] => Array ( [t] => 2016-08-31 01:42 [v] => 0.017 ) [18] => Array ( [t] => 2016-08-31 01:48 [v] => -0.007 ) [19] => Array ( [t] => 2016-08-31 01:54 [v] => -0.023 ) [20] => Array ( [t] => 2016-08-31 02:00 [v] => -0.031 ) [21] => Array ( [t] => 2016-08-31 02:06 [v] => -0.031 ) [22] => Array ( [t] => 2016-08-31 02:12 [v] => -0.024 ) [23] => Array ( [t] => 2016-08-31 02:18 [v] => -0.008 ) [24] => Array ( [t] => 2016-08-31 02:24 [v] => 0.015 ) [25] => Array ( [t] => 2016-08-31 02:30 [v] => 0.046 ) [26] => Array ( [t] => 2016-08-31 02:36 [v] => 0.084 ) [27] => Array ( [t] => 2016-08-31 02:42 [v] => 0.129 ) [28] => Array ( [t] => 2016-08-31 02:48 [v] => 0.181 ) [29] => Array ( [t] => 2016-08-31 02:54 [v] => 0.240 ) [30] => Array ( [t] => 2016-08-31 03:00 [v] => 0.305 ) [31] => Array ( [t] => 2016-08-31 03:06 [v] => 0.377 ) [32] => Array ( [t] => 2016-08-31 03:12 [v] => 0.454 ) [33] => Array ( [t] => 2016-08-31 03:18 [v] => 0.537 ) [34] => Array ( [t] => 2016-08-31 03:24 [v] => 0.625 ) [35] => Array ( [t] => 2016-08-31 03:30 [v] => 0.718 ) [36] => Array ( [t] => 2016-08-31 03:36 [v] => 0.816 ) [37] => Array ( [t] => 2016-08-31 03:42 [v] => 0.919 ) [38] => Array ( [t] => 2016-08-31 03:48 [v] => 1.025 ) [39] => Array ( [t] => 2016-08-31 03:54 [v] => 1.136 ) [40] => Array ( [t] => 2016-08-31 04:00 [v] => 1.251 ) [41] => Array ( [t] => 2016-08-31 04:06 [v] => 1.370 ) [42] => Array ( [t] => 2016-08-31 04:12 [v] => 1.492 ) [43] => Array ( [t] => 2016-08-31 04:18 [v] => 1.617 ) [44] => Array ( [t] => 2016-08-31 04:24 [v] => 1.746 ) [45] => Array ( [t] => 2016-08-31 04:30 [v] => 1.877 ) [46] => Array ( [t] => 2016-08-31 04:36 [v] => 2.010 ) [47] => Array ( [t] => 2016-08-31 04:42 [v] => 2.146 ) [48] => Array ( [t] => 2016-08-31 04:48 [v] => 2.284 ) [49] => Array ( [t] => 2016-08-31 04:54 [v] => 2.424 ) [50] => Array ( [t] => 2016-08-31 05:00 [v] => 2.566 ) [51] => Array ( [t] => 2016-08-31 05:06 [v] => 2.708 ) [52] => Array ( [t] => 2016-08-31 05:12 [v] => 2.851 ) [53] => Array ( [t] => 2016-08-31 05:18 [v] => 2.995 ) [54] => Array ( [t] => 2016-08-31 05:24 [v] => 3.139 ) [55] => Array ( [t] => 2016-08-31 05:30 [v] => 3.282 ) [56] => Array ( [t] => 2016-08-31 05:36 [v] => 3.425 ) [57] => Array ( [t] => 2016-08-31 05:42 [v] => 3.566 ) [58] => Array ( [t] => 2016-08-31 05:48 [v] => 3.705 ) [59] => Array ( [t] => 2016-08-31 05:54 [v] => 3.842 ) [60] => Array ( [t] => 2016-08-31 06:00 [v] => 3.977 ) [61] => Array ( [t] => 2016-08-31 06:06 [v] => 4.108 ) [62] => Array ( [t] => 2016-08-31 06:12 [v] => 4.236 ) [63] => Array ( [t] => 2016-08-31 06:18 [v] => 4.359 ) [64] => Array ( [t] => 2016-08-31 06:24 [v] => 4.478 ) [65] => Array ( [t] => 2016-08-31 06:30 [v] => 4.592 ) [66] => Array ( [t] => 2016-08-31 06:36 [v] => 4.700 ) [67] => Array ( [t] => 2016-08-31 06:42 [v] => 4.802 ) [68] => Array ( [t] => 2016-08-31 06:48 [v] => 4.898 ) [69] => Array ( [t] => 2016-08-31 06:54 [v] => 4.988 ) [70] => Array ( [t] => 2016-08-31 07:00 [v] => 5.070 ) [71] => Array ( [t] => 2016-08-31 07:06 [v] => 5.145 ) [72] => Array ( [t] => 2016-08-31 07:12 [v] => 5.213 ) [73] => Array ( [t] => 2016-08-31 07:18 [v] => 5.273 ) [74] => Array ( [t] => 2016-08-31 07:24 [v] => 5.325 ) [75] => Array ( [t] => 2016-08-31 07:30 [v] => 5.370 ) [76] => Array ( [t] => 2016-08-31 07:36 [v] => 5.406 ) [77] => Array ( [t] => 2016-08-31 07:42 [v] => 5.434 ) [78] => Array ( [t] => 2016-08-31 07:48 [v] => 5.454 ) [79] => Array ( [t] => 2016-08-31 07:54 [v] => 5.467 ) [80] => Array ( [t] => 2016-08-31 08:00 [v] => 5.471 ) [81] => Array ( [t] => 2016-08-31 08:06 [v] => 5.467 ) [82] => Array ( [t] => 2016-08-31 08:12 [v] => 5.455 ) [83] => Array ( [t] => 2016-08-31 08:18 [v] => 5.436 ) [84] => Array ( [t] => 2016-08-31 08:24 [v] => 5.409 ) [85] => Array ( [t] => 2016-08-31 08:30 [v] => 5.375 ) [86] => Array ( [t] => 2016-08-31 08:36 [v] => 5.334 ) [87] => Array ( [t] => 2016-08-31 08:42 [v] => 5.285 ) [88] => Array ( [t] => 2016-08-31 08:48 [v] => 5.230 ) [89] => Array ( [t] => 2016-08-31 08:54 [v] => 5.169 ) [90] => Array ( [t] => 2016-08-31 09:00 [v] => 5.101 ) [91] => Array ( [t] => 2016-08-31 09:06 [v] => 5.027 ) [92] => Array ( [t] => 2016-08-31 09:12 [v] => 4.947 ) [93] => Array ( [t] => 2016-08-31 09:18 [v] => 4.862 ) [94] => Array ( [t] => 2016-08-31 09:24 [v] => 4.771 ) [95] => Array ( [t] => 2016-08-31 09:30 [v] => 4.675 ) [96] => Array ( [t] => 2016-08-31 09:36 [v] => 4.575 ) [97] => Array ( [t] => 2016-08-31 09:42 [v] => 4.471 ) [98] => Array ( [t] => 2016-08-31 09:48 [v] => 4.362 ) [99] => Array ( [t] => 2016-08-31 09:54 [v] => 4.250 ) [100] => Array ( [t] => 2016-08-31 10:00 [v] => 4.134 ) [101] => Array ( [t] => 2016-08-31 10:06 [v] => 4.015 ) [102] => Array ( [t] => 2016-08-31 10:12 [v] => 3.893 ) [103] => Array ( [t] => 2016-08-31 10:18 [v] => 3.768 ) [104] => Array ( [t] => 2016-08-31 10:24 [v] => 3.642 ) [105] => Array ( [t] => 2016-08-31 10:30 [v] => 3.513 ) [106] => Array ( [t] => 2016-08-31 10:36 [v] => 3.383 ) [107] => Array ( [t] => 2016-08-31 10:42 [v] => 3.251 ) [108] => Array ( [t] => 2016-08-31 10:48 [v] => 3.119 ) [109] => Array ( [t] => 2016-08-31 10:54 [v] => 2.986 ) [110] => Array ( [t] => 2016-08-31 11:00 [v] => 2.852 ) [111] => Array ( [t] => 2016-08-31 11:06 [v] => 2.718 ) [112] => Array ( [t] => 2016-08-31 11:12 [v] => 2.584 ) [113] => Array ( [t] => 2016-08-31 11:18 [v] => 2.450 ) [114] => Array ( [t] => 2016-08-31 11:24 [v] => 2.317 ) [115] => Array ( [t] => 2016-08-31 11:30 [v] => 2.185 ) [116] => Array ( [t] => 2016-08-31 11:36 [v] => 2.054 ) [117] => Array ( [t] => 2016-08-31 11:42 [v] => 1.924 ) [118] => Array ( [t] => 2016-08-31 11:48 [v] => 1.796 ) [119] => Array ( [t] => 2016-08-31 11:54 [v] => 1.669 ) [120] => Array ( [t] => 2016-08-31 12:00 [v] => 1.545 ) [121] => Array ( [t] => 2016-08-31 12:06 [v] => 1.423 ) [122] => Array ( [t] => 2016-08-31 12:12 [v] => 1.303 ) [123] => Array ( [t] => 2016-08-31 12:18 [v] => 1.186 ) [124] => Array ( [t] => 2016-08-31 12:24 [v] => 1.073 ) [125] => Array ( [t] => 2016-08-31 12:30 [v] => 0.963 ) [126] => Array ( [t] => 2016-08-31 12:36 [v] => 0.856 ) [127] => Array ( [t] => 2016-08-31 12:42 [v] => 0.754 ) [128] => Array ( [t] => 2016-08-31 12:48 [v] => 0.656 ) [129] => Array ( [t] => 2016-08-31 12:54 [v] => 0.563 ) [130] => Array ( [t] => 2016-08-31 13:00 [v] => 0.474 ) [131] => Array ( [t] => 2016-08-31 13:06 [v] => 0.391 ) [132] => Array ( [t] => 2016-08-31 13:12 [v] => 0.314 ) [133] => Array ( [t] => 2016-08-31 13:18 [v] => 0.242 ) [134] => Array ( [t] => 2016-08-31 13:24 [v] => 0.177 ) [135] => Array ( [t] => 2016-08-31 13:30 [v] => 0.118 ) [136] => Array ( [t] => 2016-08-31 13:36 [v] => 0.066 ) [137] => Array ( [t] => 2016-08-31 13:42 [v] => 0.021 ) [138] => Array ( [t] => 2016-08-31 13:48 [v] => -0.016 ) [139] => Array ( [t] => 2016-08-31 13:54 [v] => -0.047 ) [140] => Array ( [t] => 2016-08-31 14:00 [v] => -0.069 ) [141] => Array ( [t] => 2016-08-31 14:06 [v] => -0.084 ) [142] => Array ( [t] => 2016-08-31 14:12 [v] => -0.091 ) [143] => Array ( [t] => 2016-08-31 14:18 [v] => -0.091 ) [144] => Array ( [t] => 2016-08-31 14:24 [v] => -0.082 ) [145] => Array ( [t] => 2016-08-31 14:30 [v] => -0.066 ) [146] => Array ( [t] => 2016-08-31 14:36 [v] => -0.041 ) [147] => Array ( [t] => 2016-08-31 14:42 [v] => -0.009 ) [148] => Array ( [t] => 2016-08-31 14:48 [v] => 0.030 ) [149] => Array ( [t] => 2016-08-31 14:54 [v] => 0.077 ) [150] => Array ( [t] => 2016-08-31 15:00 [v] => 0.131 ) [151] => Array ( [t] => 2016-08-31 15:06 [v] => 0.192 ) [152] => Array ( [t] => 2016-08-31 15:12 [v] => 0.260 ) [153] => Array ( [t] => 2016-08-31 15:18 [v] => 0.334 ) [154] => Array ( [t] => 2016-08-31 15:24 [v] => 0.415 ) [155] => Array ( [t] => 2016-08-31 15:30 [v] => 0.502 ) [156] => Array ( [t] => 2016-08-31 15:36 [v] => 0.594 ) [157] => Array ( [t] => 2016-08-31 15:42 [v] => 0.692 ) [158] => Array ( [t] => 2016-08-31 15:48 [v] => 0.796 ) [159] => Array ( [t] => 2016-08-31 15:54 [v] => 0.904 ) [160] => Array ( [t] => 2016-08-31 16:00 [v] => 1.016 ) [161] => Array ( [t] => 2016-08-31 16:06 [v] => 1.134 ) [162] => Array ( [t] => 2016-08-31 16:12 [v] => 1.255 ) [163] => Array ( [t] => 2016-08-31 16:18 [v] => 1.381 ) [164] => Array ( [t] => 2016-08-31 16:24 [v] => 1.510 ) [165] => Array ( [t] => 2016-08-31 16:30 [v] => 1.642 ) [166] => Array ( [t] => 2016-08-31 16:36 [v] => 1.778 ) [167] => Array ( [t] => 2016-08-31 16:42 [v] => 1.917 ) [168] => Array ( [t] => 2016-08-31 16:48 [v] => 2.059 ) [169] => Array ( [t] => 2016-08-31 16:54 [v] => 2.203 ) [170] => Array ( [t] => 2016-08-31 17:00 [v] => 2.349 ) [171] => Array ( [t] => 2016-08-31 17:06 [v] => 2.498 ) [172] => Array ( [t] => 2016-08-31 17:12 [v] => 2.648 ) [173] => Array ( [t] => 2016-08-31 17:18 [v] => 2.799 ) [174] => Array ( [t] => 2016-08-31 17:24 [v] => 2.952 ) [175] => Array ( [t] => 2016-08-31 17:30 [v] => 3.105 ) [176] => Array ( [t] => 2016-08-31 17:36 [v] => 3.258 ) [177] => Array ( [t] => 2016-08-31 17:42 [v] => 3.412 ) [178] => Array ( [t] => 2016-08-31 17:48 [v] => 3.564 ) [179] => Array ( [t] => 2016-08-31 17:54 [v] => 3.716 ) [180] => Array ( [t] => 2016-08-31 18:00 [v] => 3.867 ) [181] => Array ( [t] => 2016-08-31 18:06 [v] => 4.016 ) [182] => Array ( [t] => 2016-08-31 18:12 [v] => 4.162 ) [183] => Array ( [t] => 2016-08-31 18:18 [v] => 4.306 ) [184] => Array ( [t] => 2016-08-31 18:24 [v] => 4.446 ) [185] => Array ( [t] => 2016-08-31 18:30 [v] => 4.582 ) [186] => Array ( [t] => 2016-08-31 18:36 [v] => 4.715 ) [187] => Array ( [t] => 2016-08-31 18:42 [v] => 4.842 ) [188] => Array ( [t] => 2016-08-31 18:48 [v] => 4.964 ) [189] => Array ( [t] => 2016-08-31 18:54 [v] => 5.081 ) [190] => Array ( [t] => 2016-08-31 19:00 [v] => 5.192 ) [191] => Array ( [t] => 2016-08-31 19:06 [v] => 5.296 ) [192] => Array ( [t] => 2016-08-31 19:12 [v] => 5.393 ) [193] => Array ( [t] => 2016-08-31 19:18 [v] => 5.484 ) [194] => Array ( [t] => 2016-08-31 19:24 [v] => 5.567 ) [195] => Array ( [t] => 2016-08-31 19:30 [v] => 5.642 ) [196] => Array ( [t] => 2016-08-31 19:36 [v] => 5.710 ) [197] => Array ( [t] => 2016-08-31 19:42 [v] => 5.770 ) [198] => Array ( [t] => 2016-08-31 19:48 [v] => 5.821 ) [199] => Array ( [t] => 2016-08-31 19:54 [v] => 5.865 ) [200] => Array ( [t] => 2016-08-31 20:00 [v] => 5.900 ) [201] => Array ( [t] => 2016-08-31 20:06 [v] => 5.927 ) [202] => Array ( [t] => 2016-08-31 20:12 [v] => 5.946 ) [203] => Array ( [t] => 2016-08-31 20:18 [v] => 5.956 ) [204] => Array ( [t] => 2016-08-31 20:24 [v] => 5.959 ) [205] => Array ( [t] => 2016-08-31 20:30 [v] => 5.954 ) [206] => Array ( [t] => 2016-08-31 20:36 [v] => 5.940 ) [207] => Array ( [t] => 2016-08-31 20:42 [v] => 5.919 ) [208] => Array ( [t] => 2016-08-31 20:48 [v] => 5.890 ) [209] => Array ( [t] => 2016-08-31 20:54 [v] => 5.854 ) [210] => Array ( [t] => 2016-08-31 21:00 [v] => 5.810 ) [211] => Array ( [t] => 2016-08-31 21:06 [v] => 5.760 ) [212] => Array ( [t] => 2016-08-31 21:12 [v] => 5.702 ) [213] => Array ( [t] => 2016-08-31 21:18 [v] => 5.638 ) [214] => Array ( [t] => 2016-08-31 21:24 [v] => 5.567 ) [215] => Array ( [t] => 2016-08-31 21:30 [v] => 5.490 ) [216] => Array ( [t] => 2016-08-31 21:36 [v] => 5.407 ) [217] => Array ( [t] => 2016-08-31 21:42 [v] => 5.318 ) [218] => Array ( [t] => 2016-08-31 21:48 [v] => 5.224 ) [219] => Array ( [t] => 2016-08-31 21:54 [v] => 5.125 ) [220] => Array ( [t] => 2016-08-31 22:00 [v] => 5.021 ) [221] => Array ( [t] => 2016-08-31 22:06 [v] => 4.912 ) [222] => Array ( [t] => 2016-08-31 22:12 [v] => 4.798 ) [223] => Array ( [t] => 2016-08-31 22:18 [v] => 4.681 ) [224] => Array ( [t] => 2016-08-31 22:24 [v] => 4.560 ) [225] => Array ( [t] => 2016-08-31 22:30 [v] => 4.435 ) [226] => Array ( [t] => 2016-08-31 22:36 [v] => 4.307 ) [227] => Array ( [t] => 2016-08-31 22:42 [v] => 4.176 ) [228] => Array ( [t] => 2016-08-31 22:48 [v] => 4.043 ) [229] => Array ( [t] => 2016-08-31 22:54 [v] => 3.908 ) [230] => Array ( [t] => 2016-08-31 23:00 [v] => 3.771 ) [231] => Array ( [t] => 2016-08-31 23:06 [v] => 3.632 ) [232] => Array ( [t] => 2016-08-31 23:12 [v] => 3.492 ) [233] => Array ( [t] => 2016-08-31 23:18 [v] => 3.351 ) [234] => Array ( [t] => 2016-08-31 23:24 [v] => 3.209 ) [235] => Array ( [t] => 2016-08-31 23:30 [v] => 3.067 ) [236] => Array ( [t] => 2016-08-31 23:36 [v] => 2.925 ) [237] => Array ( [t] => 2016-08-31 23:42 [v] => 2.783 ) [238] => Array ( [t] => 2016-08-31 23:48 [v] => 2.642 ) [239] => Array ( [t] => 2016-08-31 23:54 [v] => 2.502 ) )
这是我的代码。有时它返回正确的结果,但有时不会。我究竟做错了什么?
$high_low = $high_final = $low_final = array();
$high = $low = $result_obj[0]['v'];
$high_flag=true; $low_flag=true;
for($i=0; $i<count($result_obj); $i++){
$todays_h_l = $result_obj[$i];
$high_low[$todays_h_l['t']] = $todays_h_l['v'];
if( $low_flag && $todays_h_l['v'] < $low ){
$low = $todays_h_l['v'];
if(!isset($result_obj[$i+1]) || (isset($result_obj[$i+1]) && $result_obj[$i+1]['v'] > $low)){
$low_final[$todays_h_l['t']] = $todays_h_l['v'];
$low = 1;
$low_flag=false; $high_flag=true;
}
}
if( $high_flag && $todays_h_l['v'] > $high ){
$high = $todays_h_l['v'];
if(!isset($result_obj[$i+1]) || (isset($result_obj[$i+1]) && $result_obj[$i+1]['v'] < $high)){
$high_final[$todays_h_l['t']] = $todays_h_l['v'];
$high = 4;
$high_flag=false; $low_flag=true;
}
}
}
答案 0 :(得分:0)
也许这对你的问题更有效。
//example data
$result_obj = array(
['v'=>1],['v'=>2],['v'=>3.1],['v'=>2],['v'=>1],['v'=>0.1],['v'=>1],
['v'=>2],['v'=>3.2],['v'=>2],['v'=>1],['v'=>0.2],['v'=>1],['v'=>2],
['v'=>3.3],['v'=>2],['v'=>1],['v'=>0.3],['v'=>1]);
//test up or down - here it means the value grows up
$lookupWay = (bool)(($result_obj[0]['v']<$result_obj[1]['v']));
$collectMAX=array();
$collectMIN=array();
foreach($result_obj as $idx=>$set){
//we lookup for max, because data is grow up
if($lookupWay && isset($result_obj[$idx+1])){
//data goes back down, so current is max
if($set['v']>$result_obj[$idx+1]['v']){
$collectMAX[]=$set['v'];
$lookupWay=false;
}
//we lookup for min
} else if(isset($result_obj[$idx+1])) {
//data goes back up,current is min
if($set['v']<$result_obj[$idx+1]['v']){
$collectMIN[]=$set['v'];
$lookupWay=true;
}
}
}
var_dump($collectMAX);//holds all high peaks
var_dump($collectMIN);//holds all low peaks
但是你需要对它进行微调。对于第一个值相等的情况,等等......