Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alerting Javascript array object shows empty

I am trying to send the JavaScript array to my php, but php gets empty [].
It even shows like that in my browser. I have always sent JSON and had no problem, but now have this format.

I have this example that makes no sense...it's just a code to illustrate the issue:

var blah    = [];
var letters = ['a', 'b', 'c', 'd'];

for (var i = 0; i < letters.length; i++)
{
    blah[letters[i]] = i;
}

Inside firebug DOM it shows as follows:

blah      []
    a      0
    b      1
    c      2
    d      3

When I do

  1. alert(blah) ------------------------------- I get empty
  2. alert(JSON.stringify(blah)) ----- I get []
  3. alert(blah.a) ---------------------------- I get 0

So how can I pass this object to php? Thanks

like image 439
Brian Avatar asked Jun 09 '26 14:06

Brian


2 Answers

Instead of an array, try using an object.

So instead of:

var blah    = [];

Try

var blah    = {};
like image 133
rossipedia Avatar answered Jun 11 '26 02:06

rossipedia


Arrays are continuously numerically indexed. They do not have strings as key names.
What you want is an object {}, not an array [].

like image 27
deceze Avatar answered Jun 11 '26 03:06

deceze