持っているもの1つのエントリが配列に追加されています 複数の値を追加していますが、他の ワードでは関数がいくつか実行されます。 回、次にそれを の配列全体に送信したいのですが、これはちょうど すべてをカンマで区切記号 に追加してください。
ここで正確に何を言っているのかわかりません。前に与えた第2の例は、それぞれuid
に対して1つのx、yペアがあると仮定しますが、uid
の数が_tags
である数に制限はありません。それはなぜvar _tags = {};
が関数の - 私たちのグローバル変数です。
以下の修正では、各uid
のために複数のx、yのペアを持つことができるようになる:
function add_tag_queue(uid,x,y){
/*
* detect if _tags[uid] already exists with one or more values
* we assume if its not undefined then the value is an array...
* this is similar to doing isset($_tags[$uid]) in php
*/
if(typeof _tags[uid] == 'undefined'){
/*
* use an array literal by enclosing the value in [],
* this makes _tags[uid] and array with eah element of
* that array being a hash with an x and y value
*/
_tags[uid] = [{'x':x,'y':y}];
} else {
// if _tags[uid] is already defined push the new x,y onto it
_tags[uid].push({'x':x, 'y':y});
}
}
これは動作するはずです:
function add_tag_queue(uid,x,y){
_tags.push([uid, x,y]);
}
キーとしてuid
をしたい場合そうでないオブジェクト/ハッシュを使用する必要がありますarray
var _tags = {}; // tags is an object
function add_tag_queue(uid,x,y){
_tags[uid] = {'x':x,'y':y};
}
これは、限り、アレイに追加されたエントリが1つしか自分であるとして、私は複数の値を追加している、つまり関数が数回実行され、その後、私はその配列全体を送信したい[OK]を動作しますこれはカンマ区切りですべてを追加しているようです。 – Brian
私が思う第2の方法は、私が望むものですが、PHPで配列に変換する方法はありますか? – Brian
'json_decode'はjson文字列をphp配列にデコードします。 http://php.net/manual/en/function.json-decode.php – prodigitalson