Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had some issues with a recursive solution to this since I am also learning [setting the count to zero as shown above will erase your current count on each recursive call, and I haven't solved reference / scope errors when passing counts as function arguments].</p> <p>Also, I had to modify the notation of your original array definition to get my test working.<br> A) I assumed your keys were meant to be string literals, not variables or constants. So I added '' wrappers. If they were meant to be variables defined elsewhere, add your '$'..<br> B) You need to comma-separate your list of elements within a single array, which I added<br> C) Not technically necessary, but I do not use bracket notation while defining, only when accessing by key after creation. Developer's choice but it helps me determine if I am ( creating or using existing ) array elements when I go back to the code later.<br> D) Don't forget to include your ';' after defining the data array, array defs are not self-closing like function or conditional code {} structure</p> <p>Admittedly, this is a slightly less robust solution than recursion, but it gives you full access to every element by key and by value, at which point you can report or manipulate values as needed. The restriction in this method is you need to know how many levels of nesting (and therefore how many foreach loops). The upshot is adding elements to existing array levels will not affect the loop logic.</p> <pre><code>&lt;?php function html_pp ( $text ) // no return { // echo paragraph to browser echo PHP_EOL . '&lt;p&gt;' . Sprintf ( $text ) . '&lt;/p&gt;' . PHP_EOL; } // Data array $data = Array ( 'Nov 18, 2011' =&gt; Array ( 'C' =&gt; Array ( 'C' =&gt; Array ( 'T' =&gt; -1324.388328 ), // comma 'S' =&gt; Array ( 'T' =&gt; -249.976472 ) ) ), // comma 'Dec 24, 2011' =&gt; Array ( 'C' =&gt; Array ( 'C' =&gt; Array ( 'T' =&gt; -2523.107928 ), // comma 'S' =&gt; Array ( 'T' =&gt; 103.533528 ) ) ), // comma 'Dec 27, 2011' =&gt; Array ( 'C' =&gt; Array ( 'C' =&gt; Array ( 'T' =&gt; -4558.837928 ), // comma 'S' =&gt; Array ( 'T' =&gt; -1639.376472 ) ) ) ); // end array def // Count variables $positive = 0; $negative = 0; html_pp ( 'Data Array :' ); // Loop using key =&gt; value pairs foreach ( $data as $i =&gt; $date ) // Iterate through date elements { foreach ( $date as $j =&gt; $c ) // Iterate through C elements { foreach ( $c as $k =&gt; $cs ) // Iterate through C/S elements { foreach ( $cs as $n =&gt; $t ) // Iterate though T elements { // echo current element nesting path and value html_pp ( $i . '&gt;' . $j . '&gt;' . $k . '&gt;' . $n . ' = ' . $t ); // update +- counts if ( $t &lt; 0 ) { $negative += 1; } else { $positive += 1; } } } } } html_pp ( 'Positive : ' . $positive ); html_pp ( 'Negative : ' . $negative ); ?&gt; </code></pre> <h2>Browser output:</h2> <p>Data Array :</p> <p>Nov 18, 2011>C>C>T = -1324.388328</p> <p>Nov 18, 2011>C>S>T = -249.976472</p> <p>Dec 24, 2011>C>C>T = -2523.107928</p> <p>Dec 24, 2011>C>S>T = 103.533528</p> <p>Dec 27, 2011>C>C>T = -4558.837928</p> <p>Dec 27, 2011>C>S>T = -1639.376472</p> <p>Positive : 1</p> <p>Negative : 5</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload