php 如何将项目添加到 json 文件格式的数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16184047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add item to the json file formatted array
提问by user2234992
I am using using just 1 data to insert in my json file.
我只使用 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);
So this is how my json file looks. I just want to add 1 player at the end.
所以这就是我的 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":"*"
},
]
}
]
}
My question is, how do I add player at the end? And I would also like to know how to update
我的问题是,我如何在最后添加播放器?我也想知道怎么更新
player1id":"*",
"player2id":"*",
"winner":"
in the match array.
在匹配数组中。
回答by chandresh_cool
Just decode your json string and then use array push
只需解码您的 json 字符串,然后使用数组推送
$tempArray = json_decode($jsonstring, true);
array_push($tempArray, $your_data);
For your case
对于您的情况
$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);
Just check value of print_r($arr); I hope this is what you want. :)
只需检查 print_r($arr); 的值;我希望这就是你想要的。:)
回答by slash197
Adding another player
添加另一个玩家
$tempArray = json_decode($inp, true);
array_push($tempArray['players'], array('name' => $data['username'], 'image' => $data['userimage'], 'roll_over_image' => 'tank.jpg'));
Updating matches
更新比赛
first match array
第一个匹配数组
$tempArray['games'][0]['matches'];
second match array
第二个匹配数组
$tempArray['games'][1]['matches'];
are now simple two dimensional arrays with keys player1id
, player2id
and winner
- it should be easy to update these.
After that you can encode the $tempArray
back to json.
现在是带有键的简单二维数组player1id
,player2id
并且winner
- 更新它们应该很容易。之后,您可以将$tempArray
返回编码为 json。
回答by sudheer suri
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.3.min.js" ></script>
</head>
<body>
<?php
//first copy your json data data.json
$str = file_get_contents('data.json');//get contents of your json file and store it in a string,bro small suggestion never keep any JSON data in ur html file its not safe.always keep json data in external file.
$arr = json_decode($str, true);//decode it
$arrne['players'] = "sadaadad";
$arrne['image'] = "sadaadad";
$arrne['roll_over_image'] = "sadaadad";
array_push( $arr['employees'], $arrne);//push contents to ur decoded array i.e $arr
$str = json_encode($arr);
//now send evrything to ur data.json file using folowing code
if (json_decode($str) != null)
{
$file = fopen('data.json','w');
fwrite($file, $str);
fclose($file);
}
else
{
// invalid JSON, handle the error
}
?>
</body>
回答by pankaj kumar
In Core PHP
在核心 PHP
if you want an array on JSON Response. then you can go with this code.
如果你想要一个关于 JSON 响应的数组。那么你可以使用这个代码。
Very simple way, you can use these steps.
很简单的方法,你可以使用这些步骤。
1) step you have to convert JSON into an Array using json_decode().
1) 您必须使用json_decode()将 JSON 转换为数组的步骤。
2) use array_merge() method for add new array. if you are interested to add an array.
2) 使用 array_merge() 方法添加新数组。如果您有兴趣添加一个数组。
$staff = json_decode($staffRes ,true);
$driver = ["helpers"=>[id=>1,name=>hep1],[id=>2,name=>hep2]]
$profile= array_merge($staff ,$driver );
In Laravel
在 Laravel
$staff = collect($staffRes)->toArray() ; // json() also work here.
$driver = ["helpers"=>[id=>1,name=>hep1],[id=>2,name=>hep2]]
$profile= array_merge($staff ,$driver );
OutPut
输出
{
"error": 0,
"errmsg": "",
"response": {
"id": "NlF4VDMrdEoxM2RCUWkxUE92c29tZz09",
"type": "DRIVER",
"driver_name": "Ravi Kumar",
"route_name": "Barasat Dak Bungalow",
"helpers": [
{
"helper_id": "K09NTlpHMStiNGlKSGZNMUIyWlAxZz09",
"helper_name": "Arvind Kumar",
"helper_mobile": "7777777775",
"helper_alt_mobile": "7777777777",
"birth_date": "01-10-2000",
"address": "Bongaon",
"id_proof": "123456789-WB",
"licence_no": null,
"experience": "2-year",
"helper_photo":""
},
{
"helper_id": "K29la21vY0VnMTZ5cFY2MU02cm1ZUT09",
"helper_name": "SUBIR DAS",
"helper_mobile": "5555555555",
"helper_alt_mobile": "5555555554",
"birth_date": "30-10-2019",
"address": "610/8, ....",
"id_proof": "NA",
"licence_no": "NA",
"experience": "2 years",
"helper_photo": ""
}
]
}
}