0

I try to send a javascript array to php using ajax he's what I did

var d = $("#fromDate").val();
var arr = [];
for(var x=0; x<count_select; x++){
       var myArray = new Array();
       myArray['val'] = $("select").eq(x).val();
       myArray['type'] = $("select").eq(x).attr('type');
       myArray['id'] = $("select").eq(x).attr('id');
       arr.push(myArray);
       myArray = new Array();
}

The array contains elements as I want in a multidimensional array.

Array screenshot

But when I send it to php page like this: (Array is not converted yet here's where I want to convert it to send as json)

$.ajax({
     type: "POST",
     url: "URL_PATH",
     data: {"arrar":arr,"date":d},
     dataType: "json",
        success: function(data) {
            alert(data);
        }
});

In the network tab i find

date    "2017-07-08"

and can't find the array so how can I convert this multidimensional array to an object so I can handle it using php

3
  • in js everything is an object Commented Jul 8, 2017 at 12:57
  • pz paste full code here. Commented Jul 8, 2017 at 12:57
  • Hey...Are you sure JS supports associative arrays? No it doesn't!! Commented Jul 8, 2017 at 12:59

3 Answers 3

1

Can simplify this using map() and by using an object for each item:

var arr = $('select').map(function() {
  return {
    val:  this.value,
    id:   this.id,
    type: $(this).attr('type')
  }
}).get();
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks but the result is long arr[0][val] "1" arr[0][id] "72" arr[0][type] "parent" arr[1][val] "2" arr[1][id] "8" arr[1][type] "sub" arr[2][val] "0" arr[2][id] "6" arr[2][type] "sub" date "2017-07-08" How to handle it using php as these are not fixed parameters they might be less or more
foreach(what?) I get parameters not an object to loop through they are just parameters and don't know their count
It is an array of arrays in php. If you need count it is count($_POST['arrar']) or a loop would be like : foreach( $_POST['arrar'] as $selectItem)
$array = $_POST['arr']; then used foreach to handle the data. Thanks a lot
1

var myArray = {} is object not a array .Because Array not have key value pair in js.

var arr=[];
for(var x=0; x<10; x++){
       var myArray = {};//try with object not a array
       myArray['val'] = 10;
       myArray['type'] = 11;
       myArray['id'] = 12;
       arr.push(myArray);
}

console.log(arr)

Comments

-1
var array = new Array()
array["foo"] = "bar";

console.log(JSON.stringify(array))
// -> []

Oops?

The problem here is, that your array is not going to be converted to text properly when its going to be send in the post request. Arrays in JS only expect numbers as indexes, there are no associative arrays in JS. The analogy to associative arrays are objects. where you have a simple key: value store.

What you really want is an object

var obj = {};
obj["foo"] = "bar";

console.log(JSON.stringify(obj))
// -> { "foo": "bar" }

In Javascript everything is an object, so you could do things like:

var n = 1;
n.name = "number with property";

And you would't get an error, even if that makes no sense ;)

1 Comment

Well some people voted down, but this is actually the right answer, with explanation

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.