我几乎可以将数据添加到我的文件中。但我正在尝试找出确切的语法..任何帮助都非常感谢..
我的 php 脚本
$inp = file_get_contents('jsontrail.json');
$tempArray = json_decode($inp, true);
//adding to the 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);
我的有效 json 文件
{
"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 */
]
}
]
}
我正在尝试将数据添加到已经存在的数组中,
在游戏->first_match_array->mymatch_id_input->push_data_here 中。
执行后,这就是我在屏幕上得到的
{
"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"
}
}
]
}
}
}
上面是用 array_push 得到的。我用 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"
}
}
]
}
}
}
请帮忙,看起来很简单,但找不到确切的代码来做这件事..请帮忙..