Reputation: 17
I have this JavaScript array:
testarr = new Array();
testarr[0] = new Array();
testarr[0]["#FFFFFF"] = "White";
testarr[0]["#FFFFFF"] = new Array();
testarr[0]["#FFFFFF"]["#FFFFFA"] = "A";
testarr[0]["#FFFFFF"]["#FFFFFB"] = "B";
testarr[0]["#000000"] = "Black";
testarr[0]["#000000"] = new Array();
testarr[0]["#000000"]["#000001"] = "A1";
testarr[0]["#000000"]["#000002"] = "B1";
I use this jQuery AJAX post method:
$.ajax({
type: 'POST',
url: 'backEndFile.php',
dataType: 'json',
data: {cat:testarr},
success: function(data) {
if (data.success === true) {
alert("SUCCESS");
} else {
alert("NOT SUCCESS")
}
}
});
my backEndFile.php
<?php
var_dump($_POST);
?>
In backEndFile var dump prints: array(0) {}
not found me post array.
if I change me array keys from string to digits like this:
testarr[0] = new Array();
testarr[0][0] = "White";
testarr[0][0] = new Array();
testarr[0][0][0] = "A";
testarr[0][0][1] = "B";
testarr[0][1] = "Black";
testarr[0][1] = new Array();
testarr[0][1][0] = "A1";
testarr[0][1][1] = "B1";
then prints correctly
Upvotes: 0
Views: 445
Reputation: 418
Try this,
testarr = {}
testarr[0] = {}
testarr[0]["white"] = {}
testarr[0]["white"]["#FFFFFA"] = "A";
testarr[0]["white"]["#FFFFFB"] = "B";
testarr[0]["black"] = {}
testarr[0]["black"]["#000001"] = "A1";
testarr[0]["black"]["#000002"] = "B1";
console.log(testarr[0].white);
Upvotes: 1
Reputation: 943569
JavaScript arrays are designed for holding data with sequential, numeric key names.
When jQuery serialises them for an Ajax request, it will only include the numeric keys (because it is an array, so why would you put named keys on it?).
If you want to have named keys, then use an Object
, not an Array
.
testarr = new Array();
testarr[0] = new Object();
testarr[0]["#FFFFFF"] = "White";
testarr[0]["#FFFFFF"] = new Object();
testarr[0]["#FFFFFF"]["#FFFFFA"] = "A";
testarr[0]["#FFFFFF"]["#FFFFFB"] = "B";
testarr[0]["#000000"] = "Black";
testarr[0]["#000000"] = new Object();
testarr[0]["#000000"]["#000001"] = "A1";
testarr[0]["#000000"]["#000002"] = "B1";
Note that you are overwriting your White and Black values with arrays. You probably don't want to do that.
(In general, you should also prefer the literal syntax ([]
/{}
) to the constructor syntax (new Array
/new Object
)).
testarr = [];
testarr[0] = {};
testarr[0]["#FFFFFF"] = "White";
testarr[0]["#FFFFFF"] = {};
testarr[0]["#FFFFFF"]["#FFFFFA"] = "A";
testarr[0]["#FFFFFF"]["#FFFFFB"] = "B";
testarr[0]["#000000"] = "Black";
testarr[0]["#000000"] = {};
testarr[0]["#000000"]["#000001"] = "A1";
testarr[0]["#000000"]["#000002"] = "B1";
But then you can go the whole hog and just use literal syntax throughout:
testarr = [
{
"#FFFFFF": {
"#FFFFFA": "A",
"#FFFFFB": "B"
},
"#000000": {
"#000001": "A1",
"#000002": "B1"
}
}
];
Upvotes: 2
Reputation: 22247
Here is a more reasonable way of generating your structure:
var testarr = [{
"#FFFFFF": {
"#FFFFFA": "A",
"#FFFFFB": "B"
},
"#000000": {
"#000001": "A1",
"#000002": "B1"
}
}];
console.log(testarr[0]["#000000"]["#000001"]); // result "A1"
Upvotes: 0
Reputation: 189
Try decoding your data on your PHP side.
json_decode(YOURDATA);
or just like @tpeczek commented, try
JSON.stringify({cat:testarr})
when posting it.
Upvotes: -1