amalafrida Posted January 7, 2013 Share Posted January 7, 2013 what sort of php array enumeration must I use to get from this: Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 42 [5] => 42 [6] => 42 [7] => 42 [8] => 36 [9] => 36 [10] => 0 [11] => 36 [12] => 36 [13] => 36 [14] => 36 [15] => 36 [16] => 42 [17] => 42 [18] => 42 [19] => 42 [20] => 42 [21] => 42 [22] => 42 [23] => 0 ) to this description of the array: index 0 to index 4 = 0 index 4 to index 8 = 42 index 8 to index 16 = 36 index 16 to index 23 = 42 index 23 = 0 my head is swirling with conditionals and loops ... getting dizzy ... can't crack it. thanks in advance for any assistance. Quote Link to comment https://forums.phpfreaks.com/topic/272803-array-index-values-report/ Share on other sites More sharing options...
Jessica Posted January 7, 2013 Share Posted January 7, 2013 Use either for or foreach, and some variables. We don't do homework, and we don't write it for you. It seems fairly obvious so just try it. Quote Link to comment https://forums.phpfreaks.com/topic/272803-array-index-values-report/#findComment-1403989 Share on other sites More sharing options...
amalafrida Posted January 7, 2013 Author Share Posted January 7, 2013 Well, thanks, advanced member, I guess. However, I did not believe myself to be asking for homework help. Only perhaps a bit of lucid advice. Your reply falls remarkably short on lucidity, a bit too far on moralizing ... (don't ask us to do your homework, don't ask us to write your code for you, the answer is obvious ... do it yourself). Just a bit of advice beyond the obvious lies at the heart of my request. Yes, "foreach/for" strikes me too as altogether obvious, but the part between the braces not so obvious for a "non-freak" "non-guru" type. Will keep casting about, however, for further suggestions. Quote Link to comment https://forums.phpfreaks.com/topic/272803-array-index-values-report/#findComment-1404053 Share on other sites More sharing options...
Jessica Posted January 7, 2013 Share Posted January 7, 2013 "PHP Coding Help Do you need help with some code you wrote? Ask here! We'll get you the answers!" Did you write code? If not, then why are you posting here? If you wrote code, post it and we can help. Posting an assignment and saying you don't know what to do is not what this forum is for. Quote Link to comment https://forums.phpfreaks.com/topic/272803-array-index-values-report/#findComment-1404058 Share on other sites More sharing options...
amalafrida Posted January 7, 2013 Author Share Posted January 7, 2013 In the interest of brevity, I did not post the code that generated the array. That struck me as the courteous approach, rather than burdening the list with unnecessary verbiage. I fail to understand your assumption that this is an "assignment". And I certainly do not appreciate your censorious responses. Let me repeat: I did not post an assignment and say that I don't know what to do. I posed a problem I'd been working with for quite some time and asked for some assistance, advice. A post to "stack overflow", phrased exactly as the post that began this thread, elicited a lucid and kind respose within 15 min. ... not a full solution, but advice, respectful and patient, sufficient to permit me to solve my "non-assignment". I will no doubt work with that group in the future or at least await responses on this list from members other than yourself. Would it be incendiary of me to suggest that you look into "charm" lessons? Quote Link to comment https://forums.phpfreaks.com/topic/272803-array-index-values-report/#findComment-1404063 Share on other sites More sharing options...
amalafrida Posted January 8, 2013 Author Share Posted January 8, 2013 All of the foregoing 'impolitesse' to the contrary notwithstanding, the non-obvious (at least to me) solution appears to be the code I've posted below. Any suggestions or advice will be greatly appreciated. given this Array: ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 42 [5] => 42 [6] => 42 [7] => 42 [8] => 36 [9] => 36 [10] => 0 [11] => 36 [12] => 36 [13] => 36 [14] => 36 [15] => 36 [16] => 42 [17] => 42 [18] => 42 [19] => 42 [20] => 42 [21] => 42 [22] => 42 [23] => 0 ) how to get to this description of the array: index 0 to index 4 = 0 index 4 to index 8 = 42 index 8 to index 16 = 36 index 16 to index 23 = 42 index 23 = 0 $startIndex = 0; $endIndex = 0; $lastVal = $val[0]; for($i=1; $i<25; $i++) { if ($val[$i] == $lastVal) { $endIndex = $i; } else { if($endIndex==23) $term="00:00"; else $term=($endIndex+1) . ":00"; if($lastVal!=0) $report.=$lastVal . "<br>" . ($startIndex) . ":00 to: " . $term . "<br>"; $startIndex = $i; $endIndex = $i; $lastVal = $val[$i]; } } Quote Link to comment https://forums.phpfreaks.com/topic/272803-array-index-values-report/#findComment-1404078 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.