json - php script to push data 2d array -
i there add data file.but trying ti figure out exact syntax..any highly appreciated..
my php script
$inp = file_get_contents('jsontrail.json'); $temparray = json_decode($inp, true); //adding matchid index of matches array $matchid=3; $arrne['games']['matches'][$matchid]['winner'] ="winner"; $arrne['games']['matches'][$matchid]['player1id'] ="player1id"; $arrne['games']['matches'][$matchid]['player2id'] ="player2id"; array_push($temparray,$arrne ); $jsondata = json_encode($temparray); file_put_contents('jsontrail.json', $jsondata); print_r( $inp);
my valid json file
{ "players": [ { "name": "moldova", "image": "/images/moldova.jpg", "roll_over_image": "tank.jpg" }, /* repeat */ ], "games": [ { "matches": [ { "player1id": 0, "player2id": 1, "winner": "*" }, /* repeat */ ] }, { "matches": [ { "player1id": 0, "player2id": 1, "winner": "*" }, /* repeat */ ] } ] }
i trying add data exist array,
in games->first_match_array->mymatch_id_input->push_data_here.
after execution, in screen
{ "players": [ { "name":"moldova", "image":"\/images\/moldova.jpg", "roll_over_image":"tank.jpg" }, /* repeat */ ] , "games": [ { "matches": [ { "player1id":0, "player2id":1, "winner":"*" }, /* repeat */ ] }, { "matches": [ { "player1id":0, "player2id":1, "winner":"*" }, /* repeat */ ] } ], "0": { "games": { "matches": [ { "3": { "winner":"winner", "player1id":"player1id", "player2id":"player2id" } } ] } } }
the above got array_push..the next code got array_merge
{ "players": [ { "name":"moldova", "image":"\/images\/moldova.jpg", "roll_over_image":"tank.jpg" }, /* repeat */ ], "games": [ { "matches": [ { "player1id":0, "player2id":1, "winner":"*" }, /* repeat */ ] }, { "matches": [ { "player1id":0, "player2id":1, "winner":"*" }, /* repeat */ ] } ], "0": { "games": { "matches": [ { "3": { "winner":"winner", "player1id":"player1id", "player2id":"player2id" } } ] } }, "1": { "games": { "matches": [ { "3": { "winner":"winner", "player1id":"player1id", "player2id":"player2id" } } ] } } }
please help,seems simple cannot find exact code it..please help..
the problem malformed json string, not constructed properly, try this
$str = '{ "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" } ], "games": [ { "matches": [ { "player1id": 0, "player2id": 1, "winner": "*" }, { "player1id": 2, "player2id": 3, "winner": "*" }, { "player1id": 4, "player2id": 5, "winner": "*" }, { "player1id": 6, "player2id": 7, "winner": "*" }, { "player1id": 0, "player2id": 2, "winner": "*" }, { "player1id": 4, "player2id": 6, "winner": "*" }, { "player1id": 0, "player2id": 4, "winner": 4 } ] }, { "matches": [ { "player1id": 0, "player2id": 1, "winner": "*" }, { "player1id": 2, "player2id": 3, "winner": "*" }, { "player1id": 4, "player2id": 5, "winner": "*" }, { "player1id": 6, "player2id": 7, "winner": "*" }, { "player1id": 0, "player2id": 2, "winner": "*" }, { "player1id": 4, "player2id": 6, "winner": "*" }, { "player1id": 0, "player2id": 4, "winner": 0 } ] }]}';
it work if use this.