Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you have a single result:</p> <pre><code>$result = array( 'NumberSelected' =&gt; $number, 'TargetPerc' =&gt; array(), .... ); </code></pre> <p>Then you want to add it in with some other results:</p> <pre><code>$result_set = array(); $result_set[] = $result; </code></pre> <p>At this point, <code>$result_set</code> is an array with one element, an array containing keys <code>'NumberSelected'</code>, <code>'TargetPerc'</code>, etc.</p> <p>I think you are getting tripped up with the idea of having arrays in arrays in arrays, so think of it like this: each array index is a component of an "path" to a specific piece of data, like in a filesystem. In this case, <code>$result_set</code> is the most general piece of the "path", like a drive letter on Windows. Inside our pretend drive are a bunch of folders named with numbers, so <code>$result_set[0]</code> is the first folder, <code>$result_set[1]</code> would be the second. In each of those folders are sub-folders called <code>NumberSelected</code> (actually that one would be like a file, since it has a scalar value), <code>TargetPerc</code>, etc, which correspond to the second layer of your result set: <code>$result_set[0]['NumberSelected']</code>. Now, the final dimension of your array corresponds to the individual files inside each of <code>'TargetPerc'</code>, <code>'KpiDescription'</code>, etc, where the files, unless you gave them named keys, are named with numbers.</p> <p>So, to access the first item of <code>'KpiDescription'</code> in the first result in your result set, your filesystem "path" (keeping with the analogy) would be</p> <pre><code>result_set/0/KpiDescription/0 </code></pre> <p>Now, translating it back to PHP:</p> <pre><code>$result_set[0]['KpiDescription'][0] </code></pre> <p>Easy, right?</p> <p>Now the JSON part of your question: once you <code>json_encode</code> it, it's just going to look like a big string of text to PHP, but the magic thing about <code>json_encode</code> is that it maintains the original data structure, but in Javascript.</p> <p>So your result set would look like this:</p> <pre><code>result_set = [ { NumberSelected: 123, TargetPerc: [ ... ], KpiDescription: [ ... ] } ]; </code></pre> <p>As you can see, the Javascript is just an array of objects, where each object has some properties that happen to be arrays. To access the first item in the first <code>KpiDescription</code> (just like above), you would use this:</p> <pre><code>result_set[0].KpiDescription[0] </code></pre> <p>Alternatively, because Javascript allows you to treat object properties as array indexes (actually that's not <em>quite</em> true, but that's another story), you could do this:</p> <pre><code>result_set[0]['KpiDescription'][0] </code></pre> <p>Look familiar? All it's missing from the PHP is the leading <code>$</code>.</p> <p>I hope that helps clear some things up for you.</p>
 

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