php - how do i dynamically create a multidimensional javascript array -


i'm trying dynamically build javascript array _tags globally defined array , send php via ajax request. need uid key , x,y sub array. in php like

$arr[$uid] = array('x'=>$x,'y'=>$y);  

but im having trouble figuring out array in javascript, heres have

function add_tag_queue(uid,x,y){      var arr = new array(3);     arr[0] = uid;     arr[1] = x;     arr[2] = y;      _tags.push(arr); } 

this works ok long 1 entry being added array, i'm adding multiple values, in other words function run few times , want send entire array, seems adding comma delimeter.

im not sure youre saying here. second example gave assumes there single x,y pair each uid places no limits on how many uids in _tags. thats why var _tags = {}; ourside of function - global variable.

the following modifications allow have multiple x,y pairs each uid:

function add_tag_queue(uid,x,y){     /*      * detect if _tags[uid] exists 1 or more values      * assume if not undefined value array...      * similar doing isset($_tags[$uid]) in php     */    if(typeof _tags[uid] == 'undefined'){      /*        * use array literal enclosing value in [],        * makes _tags[uid] , array eah element of        * array being hash x , y value       */      _tags[uid] = [{'x':x,'y':y}];     } else {      // if _tags[uid] defined push new x,y onto      _tags[uid].push({'x':x, 'y':y});    } } 

this should work:

function add_tag_queue(uid,x,y){      _tags.push([uid, x,y]); } 

if want uid key need use object/hash not array

var _tags = {}; // tags object function add_tag_queue(uid,x,y){          _tags[uid] = {'x':x,'y':y};     } 

Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -