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":"
マッチ配列内。
Json文字列をデコードしてから、配列Pushを使用するだけです
$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);の値を確認するだけです。これがあなたの望むものであることを願っています。 :)
別のプレーヤーを追加する
$tempArray = json_decode($inp, true);
array_Push($tempArray['players'], array('name' => $data['username'], 'image' => $data['userimage'], 'roll_over_image' => 'tank.jpg'));
試合の更新
最初の一致配列
$tempArray['games'][0]['matches'];
2番目の一致配列
$tempArray['games'][1]['matches'];
は、キーplayer1id
、player2id
、およびwinner
を持つ単純な2次元配列になりました-これらの更新は簡単です。その後、$tempArray
をエンコードしてjsonに戻すことができます。
<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>
コアPHP
jSON応答の配列が必要な場合。次に、このコードで行くことができます。
非常に簡単な方法で、これらの手順を使用できます。
1)json_decode()を使用してJSONを配列に変換する必要がある手順。
2)新しい配列を追加するには、array_merge()メソッドを使用します。配列を追加したい場合。
$staff = json_decode($staffRes ,true);
$driver = ["helpers"=>[id=>1,name=>hep1],[id=>2,name=>hep2]]
$profile= array_merge($staff ,$driver );
ララヴェルで
$staff = collect($staffRes)->toArray() ; // json() also work here.
$driver = ["helpers"=>[id=>1,name=>hep1],[id=>2,name=>hep2]]
$profile= array_merge($staff ,$driver );
出力
{
"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": ""
}
]
}
}