访问json文件中的2d数组

时间:2013-04-24 12:59:38

标签: php json file push

我只想将数据放入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"}}]}

对我来说,任何帮助都会有很大的帮助。不妨提前做好准备!!

1 个答案:

答案 0 :(得分:2)

/* last argument says for decode JSON as array */
$gameToUpdate = 0;
$arrne = json_decode(file_get_contents('7players.json'), true);
/* play with your JSON here */
/* lets suppose that there's lots of matches */
$matchToPrepend = array(
    'player1id' => "player1",
    'player2id' => "player2",
    'winner' => "winner"
);
/* this will prepend data to array */
array_unshift($arrne['matches']['games'][$gameToUpdate], $matchToPrepend);
/* save your JSON */
file_put_contents('7players.json', json_encode($arrne));

file_put_contents

file_get_contents

array_unshift

json_encode

json_decode

我认为在执行之前你需要做一些检查,比如:检查文件是否存在。