Skip to content

May 9, 2011

Passing Javascript objects in ExtJS Store’s params and becomes PHP associative array on the server side

by Joe Kuan

Suppose you need to pass a Javascript object as a parameter in ExtJS Store to PHP server side and you want to automatically convert into PHP associative array. You can simply do something like this inside the Store’s config:

baseParams: {
  "argArr[0][field1]": argArr[0].field1,
  "argArr[0][field2]": argArr[0].field2,

Then on the PHP server side, it will become an associative array.

Here is a Javascript function for converting the content of an object and putting it into a Store’s baseParams/params property. The prefix is the name to be the associative array on the server side.

function toPhpAssocArr(obj, prefix, params) {
  for (var i in obj) {
    if (typeof obj[i] === 'object') {
      toPhpAssocArr(obj[i], prefix + "[" + i + "]", params);
    } else if (typeof obj[i] === 'string' ||
               typeof obj[i] === 'number' ||
               typeof obj[i] === 'boolean') {
      params[prefix + '[' + i + ']'] = obj[i];

To use the above function to generate output as the first example, call it as

toPhpAssocArr(argArr, "argArr", store.baseParams);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

%d bloggers like this: