0

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

10
  • Tip: Replace your me with my. It's unrelated, but it's worth it fixing them. Commented Aug 20, 2014 at 14:30
  • Try serializing your object to JSON, use JSON.stringify: data: JSON.stringify({cat:testarr}). Currently jQquery might be seriliazing your object to url encoded form. Commented Aug 20, 2014 at 14:33
  • i try: data: JSON.stringify({cat:testarr}) doesn't get result.. Commented Aug 20, 2014 at 14:37
  • Javascript arrays don't have string indices, you appear to require an array containing objects. Also, you overwrite the value "Black" with new Array() on the next line. Commented Aug 20, 2014 at 14:40
  • To properly validate the JSON your are receiving in PHP try following: echo file_get_contents('php://input'); Commented Aug 20, 2014 at 14:43

4 Answers 4

2

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"
                }
        }
];
Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

0

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"

Comments

-1

Try decoding your data on your PHP side.

json_decode(YOURDATA);

or just like @tpeczek commented, try JSON.stringify({cat:testarr}) when posting it.

1 Comment

i try it doesn't work.. already get result empty array: array(0) {}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.