Jump to content

array index / values report


amalafrida

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

"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. 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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];
	   }
    }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.