steven mcdowell
steven mcdowell

Reputation: 573

How to access a multi Dimensional PHP Array in javascript as json-encoded variant?

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.

Upvotes: 3

Views: 1085

Answers (4)

Austin Hyde
Austin Hyde

Reputation: 27446

If you have a single result:

$result = array(
    'NumberSelected' => $number,
    'TargetPerc' => array(),
    ....
);

Then you want to add it in with some other results:

$result_set = array();
$result_set[] = $result;

At this point, $result_set is 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_set 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 $result_set[0] is the first folder, $result_set[1] would be the second. In each of those folders are sub-folders called NumberSelected (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 be

result_set/0/KpiDescription/0

Now, translating it back to PHP:

$result_set[0]['KpiDescription'][0]

Easy, right?

Now the JSON part of your question: once you json_encode it, it's just going to look like a big string of text to PHP, but the magic thing about json_encode is that it maintains the original data structure, but in Javascript.

So your result set would look like this:

result_set = [
    {
        NumberSelected: 123,
        TargetPerc: [ ... ],
        KpiDescription: [ ... ]
    }
];

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:

result_set[0].KpiDescription[0]

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:

result_set[0]['KpiDescription'][0]

Look familiar? All it's missing from the PHP is the leading $.

I hope that helps clear some things up for you.

Upvotes: 4

Inoryy
Inoryy

Reputation: 8425

You don't need to set $i, $arr = array(array(array())); is enough to init 3D array.

Your code should work with result[i]['KpiName'][i]

/Edit: added quotation marks. Tested it with syntax above, should work. Tested with:

$(document).ready(function(){
    var arr = new Array();
    arr[0] = new Array();
    arr[0]['test'] = new Array();
    arr[0]['test'][1] = 5;

    alert(arr[0]['test'][1]);
});

returned 5 as expected

Upvotes: 0

Knowledge Craving
Knowledge Craving

Reputation: 7993

The actual collecting code will be:-

$result[i][KpiName][j]

where "i" & "j" are indices of the above array.

Hope it helps.

Upvotes: 0

vascowhite
vascowhite

Reputation: 18430

$result = array(
  'NumberSelected' => $number,
  'TargetPerc'     => array (),
  'KpiDescription' => array (),
  'KpiName'        => array (),
  'ValuetoPrint'   => array (),
  'ValueNow'       => array (),
  'ValueCompare'   => array (),
  'Target'         => array (),
  'KpiUnits'       => array ()
);

$newArray[0] = $result;

is the simplest answer I can think of unless I'm missing something.

Upvotes: 0

Related Questions