我只想将数据放入 json 文件中..
这是我的代码
$inp = file_get_contents('7players.json');
$tempArray = json_decode($inp, true);
$arrne['matches']['player1id']="player1";
$arrne['matches']['player2id']="player2";
$arrne['matches']['winner']="winner";
array_push( $tempArray['games'], $arrne );
$jsonData = json_encode($tempArray);
file_put_contents('7players.json', $jsonData);
print_r( $inp);
它正在添加到 json 文件中..但它正在添加到最后..
{"players":[{"name":"Moldova","image":"\/Images\/Moldova.jpg","roll_over_image":"tank.jpg"},
{"name":"Georgia","image":"\/Images\/georgia.gif","roll_over_image":"tank.jpg"},{"name":"Belarus","image":"\/Images\/Belarus.gif","roll_over_image":"tank.jpg"},{"name":"Armenia","image":"\/Images\/armenia.png","roll_over_image":"tank.jpg"},{"name":"Kazahstan","image":"\/Images\/kazahstan.gif","roll_over_image":"tank.jpg"},{"name":"Russia","image":"\/Images\/russia.gif","roll_over_image":"tank.jpg"},{"name":"Ukraine","image":"\/Images\/ukraine.png","roll_over_image":"tank.jpg"},{"name":"Uzbekistan","image":"\/Images\/uzbekistan.gif","roll_over_image":"tank.jpg"},{"name":"nisha5"},{"name":"nisha1"}],
"games":[{"matches":[{"player1id":"*","player2id":"*","winner":"*"}, {"player1id":"*","player2id":"*","winner":"*"},{"player1id":"*","player2id":"*","winner":"*"},{"player1id":"*","player2id":7,"winner":"*"},{"player1id":"*","player2id":"*","winner":"*"},{"player1id":"*","player2id":"*","winner":"*"},{"player1id":"*","player2id":"*","winner":"*"}]},
{"matches":[{"player1id":"*","player2id":"*","winner":"*"},{"player1id":"*","player2id":"*","winner":"*"},{"player1id":"*","player2id":"*","winner":"*"},{"player1id":"*","player2id":7,"winner":"*"},{"player1id":"*","player2id":"*","winner":"*"},{"player1id":"*","player2id":"*","winner":"*"},{"player1id":"*","player2id":"*","winner":"*"}]}]}
但是添加后,文件是这样的,最后..
{"players":[{"name":"Moldova","image":"\/Images\/Moldova.jpg","roll_over_image":"tank.jpg"},{"name":"Georgia","image":"\/Images\/georgia.gif","roll_over_image":"tank.jpg"},{"name":"Belarus","image":"\/Images\/Belarus.gif","roll_over_image":"tank.jpg"},{"name":"Armenia","image":"\/Images\/armenia.png","roll_over_image":"tank.jpg"},{"name":"Kazahstan","image":"\/Images\/kazahstan.gif","roll_over_image":"tank.jpg"},{"name":"Russia","image":"\/Images\/russia.gif","roll_over_image":"tank.jpg"},{"name":"Ukraine","image":"\/Images\/ukraine.png","roll_over_image":"tank.jpg"},{"name":"Uzbekistan","image":"\/Images\/uzbekistan.gif","roll_over_image":"tank.jpg"},{"name":"nisha5"},{"name":"nisha1"}],
"games":[{"matches":[{"player1id":"*","player2id":"*","winner":"*"},{"player1id":"*","player2id":"*","winner":"*"},{"player1id":"*","player2id":"*","winner":"*"},{"player1id":"*","player2id":7,"winner":"*"},{"player1id":"*","player2id":"*","winner":"*"},{"player1id":"*","player2id":"*","winner":"*"},{"player1id":"*","player2id":"*","winner":"*"}]},
{"matches":[{"player1id":"*","player2id":"*","winner":"*"},{"player1id":"*","player2id":"*","winner":"*"},{"player1id":"*","player2id":"*","winner":"*"},{"player1id":"*","player2id":7,"winner":"*"},{"player1id":"*","player2id":"*","winner":"*"},{"player1id":"*","player2id":"*","winner":"*"},{"player1id":"*","player2id":"*","winner":"*"}]},
{"matches":{"player1id":"player1","player2id":"player2","winner":"winner"}},{"matches":{"player1id":"player1","player2id":"player2","winner":"winner"}},{"matches":{"player1id":"player1","player2id":"player2","winner":"winner"}}]}
任何帮助对我都有很大用处..提前非常感谢!!