All arrays passed to PHP must be object literals. Here's an example from JS/jQuery:
var myarray = {}; //must be declared as an object literal first
myarray[fld1] = val; // then you can add elements and values
myarray[fld2] = val;
myarray[fld3] = Array(); // array assigned to an element must also be declared as object literal
etc...`
It can now be sent via Ajax in the data: parameter as follows:
data: { new_name: myarray },
PHP picks this up and reads it as a normal array without any decoding necessary. Here's an example:
$array = $_POST['new_name']; // myarray became new_name (see above)
$fld1 = array['fld1'];
$fld2 = array['fld2'];
etc...
However, when you return an array to jQuery via Ajax it must first be encoded using JSON.
Here's an example in PHP:
$return_array = json_encode($return_aray));
print_r($return_array);
And the output from that looks something like this:
{
"fname":"James",
"lname":"Feducia",
"vip":"true",
"owner":"false",
"cell_phone":"(801) 666-0909",
"email":"[email protected]",
"contact_pk":"",
"travel_agent":""
}
{again we see the object literal encoding tags} now this can be read by JS/jQuery as an array without any further action inside JS/jQuery... Here's an example in jQuery ajax:
success: function(result) {
console.log(result);
alert( "Return Values: " + result['fname'] + " " + result['lname'] );
}