一尘不染

如何将项目添加到json文件格式的数组

json

我仅使用1个数据插入json文件。

$data=$_POST['myusernamer'];

$inp = file_get_contents('7players.json');
$tempArray = json_decode($inp);
array_push($tempArray, $data);
$jsonData = json_encode($tempArray);
file_put_contents('7players.json', $jsonData);

这就是我的json文件的外观。我只想在最后添加1个播放器。

{

"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"
   },
  ],


"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":"*"
            },

      ]
    }
  ] 
}

我的问题是,如何在最后添加播放器?我也想知道如何更新

player1id":"*",
"player2id":"*",
"winner":"

在匹配数组中。


阅读 239

收藏
2020-07-27

共1个答案

一尘不染

只需解码json字符串,然后使用数组推送

$tempArray = json_decode($jsonstring, true);
array_push($tempArray, $your_data);

对于你的情况

    $str = '{

"players":[
   {

        "name":"Moldova",
        "image":"/Images/Moldova.jpg",
        "roll_over_image":"tank.jpg"
   },
   {

        "name":"Georgia",
        "image":"/Images/georgia.gif",
        "roll_over_image":"tank.jpg"
   } ]}';


 $arr = json_decode($str, true);
 $arrne['name'] = "dsds";
 array_push( $arr['players'], $arrne );
 print_r($arr);

只需检查print_r($ arr);的值即可。我希望这就是你想要的。:)

2020-07-27