Group values of an array into zones based on another PHP array

Asked

Viewed 38 times

0

Hello,

I have two arrays where the $key is the same for the two depending on the time they were registered.

The first array is a Timeline in seconds of activity and the second array is the Heart Rate Record (FC).

$arr_time = Array ( [0] => 0 [1] => 1 [2] => 3 [3] => 4 [4] => 7 [5] => 10 [6] => 12 [7] => 15 [8] => 20 [9] => 23 [10] => 27 [11] => 28 [12] => 29 [13] => 30 [14] => 31 [15] => 32 [16] => 35 [17] => 38 [18] => 42 [19] => 43 [20] => 44 [21] => 45 [22] => 46 [23] => 47 [24] => 49 [25] => 55 [26] => 57 [27] => 58 [28] => 59 [29] => 60 [30] => 61 [31] => 63 [32] => 64 [33] => 67 [34] => 69 [35] => 70 [36] => 71 [37] => 72 [38] => 73 [39] => 78 [40] => 81 [41] => 82 [42] => 83 [43] => 84 [44] => 85 [45] => 86 [46] => 93 [47] => 94 [48] => 95 [49] => 96 [50] => 97 [51] => 98 [52] => 99 [53] => 102 [54] => 103 [55] => 104 [56] => 105 [57] => 106 [58] => 109 [59] => 111 [60] => 113 [61] => 114 [62] => 115 [63] => 116 [64] => 117 [65] => 120 [66] => 121 [67] => 122 [68] => 123 [69] => 126 [70] => 127 [71] => 128 [72] => 129 [73] => 130 [74] => 134 [75] => 136 [76] => 137 [77] => 138);

$arr_fc = Array ( [0] => 89 [1] => 89 [2] => 90 [3] => 91 [4] => 91 [5] => 90 [6] => 92 [7] => 95 [8] => 98 [9] => 101 [10] => 104 [11] => 105 [12] => 106 [13] => 107 [14] => 107 [15] => 108 [16] => 107 [17] => 106 [18] => 107 [19] => 108 [20] => 108 [21] => 109 [22] => 110 [23] => 110 [24] => 111 [25] => 112 [26] => 112 [27] => 112 [28] => 112 [29] => 113 [30] => 114 [31] => 115 [32] => 115 [33] => 116 [34] => 116 [35] => 116 [36] => 117 [37] => 117 [38] => 118 [39] => 119 [40] => 119 [41] => 120 [42] => 119 [43] => 120 [44] => 120 [45] => 120 [46] => 122 [47] => 122 [48] => 122 [49] => 123 [50] => 124 [51] => 124 [52] => 125 [53] => 126 [54] => 127 [55] => 127 [56] => 127 [57] => 127 [58] => 128 [59] => 128 [60] => 128 [61] => 129 [62] => 130 [63] => 131 [64] => 131 [65] => 135 [66] => 135 [67] => 136 [68] => 137 [69] => 140 [70] => 141 [71] => 141 [72] => 141 [73] => 142 [74] => 142 [75] => 143 [76] => 143 [77] => 143);

Serialized with json_encode (I broke the first 80 values to fit here)

[0,1,3,4,7,10,12,15,20,23,27,28,29,30,31,32,35,38,42,43,44,45,46,47,49,55,57,58,59,60,61,63,64,67,69,70,71,72,73,78,81,82,83,84,85,86,93,94,95,96,97,98,99,102,103,104,105,106,109,111,113,114,115,116,117,120,121,122,123,126,127,128,129,130,134,136,137,138,139,140]

[89,89,90,91,91,90,92,95,98,101,104,105,106,107,107,108,107,106,107,108,108,109,110,110,111,112,112,112,112,113,114,115,115,116,116,116,117,117,118,119,119,120,119,120,120,120,122,122,122,123,124,124,125,126,127,127,127,127,128,128,128,129,130,131,131,135,135,136,137,140,141,141,141,142,142,143,143,143,143,143]

Now the problem: I have 5 FC zones

Z1 < 120 bpm
Z2 entre 121 e 147 bpm
Z3 entre 148 e 164 bpm
Z4 entre 165 e 177 bpm
Z5 > 178 bpm

and need to know how long the activity was performed within each FC zone.

That is, I need a delta_time between every $key based on the zona_fc. How to do it in a more practical way using IF, switch, multiple foreach...???

  • Only the format as you posted the array will "scare away" several possible answers attempts. If only they were serialized, then it would be easier to deserialize and try.

  • 1

    serialized would put a json_encode there?

  • It would be an alternative. : ) But you don’t need to ask the question if you don’t want, just a link to a gist or Pastebin would be enough.

1 answer

0

I made the code this way, I don’t know

$delta_z1 = 0;
$delta_z2 = 0;
$delta_z3 = 0;
$delta_z4 = 0;
$delta_z5 = 0;
//Faço um loop no array dos dados de FC
for($i = 1; $i < count($stream_hr); $i++){
    //Verifico no array de velocidade se o sujeito estava em movimento (considerando movimento > 2.5 m/s)
    if($stream_v[$i] * 3.6 > 2.5) {
        //Condicional para a Z1
        if($stream_hr[$i] < 121){
            $duration = ($stream_t[$i] - $stream_t[$i-1]);
            $delta_z1 = $delta_z1 + $duration;
        }
        //Condicional para a Z2
        if($stream_hr[$i] >= 121 AND $stream_hr[$i] <= 147){
            $duration = ($stream_t[$i] - $stream_t[$i-1]);
            $delta_z2 = $delta_z2 + $duration;
        }
        //Condicional para a Z3
        if($stream_hr[$i] >= 148 AND $stream_hr[$i] <= 164){
            $duration = ($stream_t[$i] - $stream_t[$i-1]);
            $delta_z3 = $delta_z3 + $duration;
        }
        //Condicional para a Z4
        if($stream_hr[$i] >= 165 AND $stream_hr[$i] <= 176){
            $duration = ($stream_t[$i] - $stream_t[$i-1]);
            $delta_z4 = $delta_z4 + $duration;
        }
        //Condicional para a Z5
        if($stream_hr[$i] > 177){
            $duration = ($stream_t[$i] - $stream_t[$i-1]);
            $delta_z5 = $delta_z5 + $duration;
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.