Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a more complicated example, but better than spaghetti code. Turn your array into XML first.</p> <pre><code>// this is your initial array $my_array = array( array("city"=&gt;309, "store"=&gt;12, "apples"=&gt;21, "oranges"=&gt;14, "lichis"=&gt;34 ), array("city"=&gt;309, "store"=&gt;13, "apples"=&gt;0, "oranges"=&gt;11, "lichis"=&gt;32 ), array("city"=&gt;309, "store"=&gt;14, "apples"=&gt;44, "oranges"=&gt;61, "lichis"=&gt;0 ), array("city"=&gt;309, "store"=&gt;15, "apples"=&gt;7, "oranges"=&gt;0, "lichis"=&gt;6 ), array("city"=&gt;309, "store"=&gt;16, "apples"=&gt;0, "oranges"=&gt;0, "lichis"=&gt;12 ), ); </code></pre> <p>Writing spaghetti code to manually isolate stores becomes a messy jungle of if statements and loops. </p> <pre><code>// might as well convert it to xml function array_to_xml( $data ) { $xml = "&lt;xml&gt;\n"; foreach( $data as $row ) { $xml .= "&lt;entry&gt;"; foreach( $row as $k =&gt; $v ) { $xml .= "&lt;{$k}&gt;{$v}&lt;/{$k}&gt;"; } $xml .= "&lt;/entry&gt;\n"; } $xml .= "&lt;/xml&gt;\n"; return( $xml ); } </code></pre> <p>At this point, <code>$xml</code> looks like this (as a string) and is more manageable:</p> <pre><code>&lt;xml&gt; &lt;entry&gt; &lt;city&gt;309&lt;/city&gt; &lt;store&gt;12&lt;/store&gt; &lt;apples&gt;21&lt;/apples&gt; &lt;oranges&gt;14&lt;/oranges&gt; &lt;lichis&gt;34&lt;/lichis&gt; &lt;/entry&gt; ...(more entries)... &lt;/xml&gt; </code></pre> <p>Now, load it into something queriable with XPath, an XML standard:</p> <pre><code>$xml = simplexml_load_string( array_to_xml( $my_array ) ); </code></pre> <p>To get the count of all the specific fruits in the city (i.e. how many apples, oranges, lichies are there in total in city 309) we need a simple but reusable summary function.</p> <pre><code>// so lets make a generic function to count specific items function count_items( $stores, $items = array() ) { $sum = array(); foreach( $stores as $store ) { foreach( $items as $k ) { if( !isset( $sum[$k] ) ) $sum[$k] = 0; $sum[$k] += $store-&gt;$k; } } return( $sum ); } </code></pre> <p>We only want city 309, and looking specifically for apples, oranges and lichis, since they are fruits:</p> <pre><code>$only_this_city = $xml-&gt;xpath("//entry[city=309]"); print_r( count_items( $only_this_city, array("apples", "oranges", "lichis")) ); </code></pre> <p>We get this:</p> <pre><code>Array ( [apples] =&gt; 72 [oranges] =&gt; 86 [lichis] =&gt; 84 ) </code></pre> <p>Secondly, to grab the values for a specific store:</p> <pre><code>$only_this_store = $xml-&gt;xpath("//entry[city=309 and store=14]"); print_r( count_items( $only_this_store, array("apples") ) ); </code></pre> <p>You get:</p> <pre><code>Array ( [apples] =&gt; 44 ) </code></pre> <p>Obviously you can request more items, or query with more complexity. Look up some docs on XPath for future queries.</p>
    singulars
    1. This table or related slice is empty.
    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