After asking a question yesterday, I’ve learned that I need to have an array in Php and use JSON to pass it to Javascript. I’ve got all that working up to a point, but I need to add one more dimension to my array and not sure how.
The current array is:
$result = array(
'NumberSelected' => $number,
'TargetPerc' => array (),
'KpiDescription' => array (),
'KpiName' => array (),
'ValuetoPrint' => array (),
'ValueNow' => array (),
'ValueCompare' => array (),
'Target' => array (),
'KpiUnits' => array ()
);
But I now need to enclose this in another dimension to sort these by group.
This is what I’ve tried:
$result = array(
$i => array(
'NumberSelected' => $number,
'TargetPerc' => array (),
'KpiDescription' => array (),
'KpiName' => array (),
'ValuetoPrint' => array (),
'ValueNow' => array (),
'ValueCompare' => array (),
'Target' => array (),
'KpiUnits' => array ()
)
);
With $i defined as 0 two lines before that. But it doesn’t seem to recognise the outermose array. Could be something to do with my collecting code which is result.KpiName[i] currently (the JSON variable is passed to this javascript function as result). And I’ve tried result.i[KpiName[i]] with no success.
Any help would be appreciated.
If you have a single result:
Then you want to add it in with some other results:
At this point,
$result_setis an array with one element, an array containing keys'NumberSelected','TargetPerc', etc.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,
$result_setis 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$result_set[0]is the first folder,$result_set[1]would be the second. In each of those folders are sub-folders calledNumberSelected(actually that one would be like a file, since it has a scalar value),TargetPerc, etc, which correspond to the second layer of your result set:$result_set[0]['NumberSelected']. Now, the final dimension of your array corresponds to the individual files inside each of'TargetPerc','KpiDescription', etc, where the files, unless you gave them named keys, are named with numbers.So, to access the first item of
'KpiDescription'in the first result in your result set, your filesystem “path” (keeping with the analogy) would beNow, translating it back to PHP:
Easy, right?
Now the JSON part of your question: once you
json_encodeit, it’s just going to look like a big string of text to PHP, but the magic thing aboutjson_encodeis that it maintains the original data structure, but in Javascript.So your result set would look like this:
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
KpiDescription(just like above), you would use this:Alternatively, because Javascript allows you to treat object properties as array indexes (actually that’s not quite true, but that’s another story), you could do this:
Look familiar? All it’s missing from the PHP is the leading
$.I hope that helps clear some things up for you.