我有一个.txt文件,其中包含用JSON编写的Windows 8应用程序列表,用于Windows 8应用程序。我希望能够有一个在线表单,我可以输入一个新的应用程序,然后将它放在JSON .txt文件中。它使用PHP file_put_contents和JSON_encode查找,但每次插入新的应用程序时,它都会将其插入新的根[],而不是将其插入已经创建的根目录中。另一件事是pretty_printing函数不起作用,有人可以帮我把JSON输入看起来很好吗?
if(isset($_POST['send'])){
$bi = $_POST['background_image'];
$ad = $_POST['description'];
$ei = $_POST['extra-images'];
$group = $_POST['group'];
$tr = $_POST['teacherReview'];
$rating = $_POST['rating'];
$st = $_POST['shortTitle'];
$ti = $_POST['tileImage'];
$title = $_POST['title'];
$vc = $_POST['verify'];
$savePath = "/var/www/kthorndahl.com/public_html/windowsstoreData/Apps.txt";
if ($group == "ms"){
$gbi = "http://kthorndahl.com/windowsstoreData/images/math/geogebra.png";
$gd = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis laoreet tempus libero vitae feugiat. Proin justo velit, luctus ornare posuere eget, pulvinar ac nibh. Integer dapibus leo in metus volutpat blandit. Morbi facilisis laoreet magna iaculis porta. Donec quis ipsum et justo faucibus lacinia. Vivamus id sapien nec dolor venenatis hendrerit. Aenean sit amet pretium quam. ";
$gi = "http://kthorndahl.com/windowsstoreData/images/math/geogebra.png";
$gk = "Math and Science";
$gst = "Math and Science";
$gt = "Math and Science";
}
else{
$gbi = "http://kthorndahl.com/windowsstoreData/images/math/geogebra.png";
$gd = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis laoreet tempus libero vitae feugiat. Proin justo velit, luctus ornare posuere eget, pulvinar ac nibh. Integer dapibus leo in metus volutpat blandit. Morbi facilisis laoreet magna iaculis porta. Donec quis ipsum et justo faucibus lacinia. Vivamus id sapien nec dolor venenatis hendrerit. Aenean sit amet pretium quam. ";
$gi = "http://kthorndahl.com/windowsstoreData/images/math/geogebra.png";
$gk = "Human Science";
$gst = "Human Science";
$gt = "Human Science";
}
$content = array();
if($vc == "Thisisaverysecretpassword123456789"){
//get the old data
$content = json_decode( file_get_contents( $savePath ) , true );
//add the new data
$content[] = array(
'backgroundImage' => $bi ,
'description' => $ad,
'extraImages' => $ei,
'group' => array('backgroundImage' => $gbi, 'description' => $gd, 'groupImage' => $gi, 'key' => $gk, 'shortTitle' => $gst, 'title' => $gt,),
'teacherReview' => $tr,
'rating' => $rating,
'shortTitle' => $st,
'tileImage' => $ti,
'title' => $title
);
//decode the new data
$content = json_encode( $content);
file_put_contents( $savePath , $content , FILE_APPEND );
}
}
更新: 我目前的输出:
[{"backgroundImage":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","description":"Nu tester jeg lige igen","extraImages":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","group":{"backgroundImage":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis laoreet tempus libero vitae feugiat. Proin justo velit, luctus ornare posuere eget, pulvinar ac nibh. Integer dapibus leo in metus volutpat blandit. Morbi facilisis laoreet magna iaculis porta. Donec quis ipsum et justo faucibus lacinia. Vivamus id sapien nec dolor venenatis hendrerit. Aenean sit amet pretium quam. ","groupImage":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","key":"Math and Science","shortTitle":"Math and Science","title":"Math and Science"},"teacherReview":null,"rating":"9","shortTitle":"Test 2","tileImage":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","title":"Test 2"}]
[{"backgroundImage":"http:\\\\\/\\\\\/kthorndahl.com\\\\\/windowsstoreData\\\\\/images\\\\\/math\\\\\/geogebra.png","description":"Tester igen","extraImages":"http:\\\\\/\\\\\/kthorndahl.com\\\\\/windowsstoreData\\\\\/images\\\\\/math\\\\\/geogebra.png","group":{"backgroundImage":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis laoreet tempus libero vitae feugiat. Proin justo velit, luctus ornare posuere eget, pulvinar ac nibh. Integer dapibus leo in metus volutpat blandit. Morbi facilisis laoreet magna iaculis porta. Donec quis ipsum et justo faucibus lacinia. Vivamus id sapien nec dolor venenatis hendrerit. Aenean sit amet pretium quam. ","groupImage":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","key":"Math and Science","shortTitle":"Math and Science","title":"Math and Science"},"teacherReview":"stadig nice","rating":"3","shortTitle":"Test 3","tileImage":"http:\\\\\/\\\\\/kthorndahl.com\\\\\/windowsstoreData\\\\\/images\\\\\/math\\\\\/geogebra.png","title":"Test 3"}]
我想要的输出:
[{"backgroundImage":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","description":"Nu tester jeg lige igen","extraImages":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","group":{"backgroundImage":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis laoreet tempus libero vitae feugiat. Proin justo velit, luctus ornare posuere eget, pulvinar ac nibh. Integer dapibus leo in metus volutpat blandit. Morbi facilisis laoreet magna iaculis porta. Donec quis ipsum et justo faucibus lacinia. Vivamus id sapien nec dolor venenatis hendrerit. Aenean sit amet pretium quam. ","groupImage":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","key":"Math and Science","shortTitle":"Math and Science","title":"Math and Science"},"teacherReview":null,"rating":"9","shortTitle":"Test 2","tileImage":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","title":"Test 2"},
{"backgroundImage":"http:\\\\\/\\\\\/kthorndahl.com\\\\\/windowsstoreData\\\\\/images\\\\\/math\\\\\/geogebra.png","description":"Tester igen","extraImages":"http:\\\\\/\\\\\/kthorndahl.com\\\\\/windowsstoreData\\\\\/images\\\\\/math\\\\\/geogebra.png","group":{"backgroundImage":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis laoreet tempus libero vitae feugiat. Proin justo velit, luctus ornare posuere eget, pulvinar ac nibh. Integer dapibus leo in metus volutpat blandit. Morbi facilisis laoreet magna iaculis porta. Donec quis ipsum et justo faucibus lacinia. Vivamus id sapien nec dolor venenatis hendrerit. Aenean sit amet pretium quam. ","groupImage":"http:\/\/kthorndahl.com\/windowsstoreData\/images\/math\/geogebra.png","key":"Math and Science","shortTitle":"Math and Science","title":"Math and Science"},"teacherReview":"stadig nice","rating":"3","shortTitle":"Test 3","tileImage":"http:\\\\\/\\\\\/kthorndahl.com\\\\\/windowsstoreData\\\\\/images\\\\\/math\\\\\/geogebra.png","title":"Test 3"}]
答案 0 :(得分:1)
请勿使用FILE_APPEND
。您已经将新条目附加到$content
,因此您需要完全重写该文件,而不是在旧数组之后添加新数组。
$content = json_decode( file_get_contents( $savePath ) , true );
//add the new data
$content[] = array(
'backgroundImage' => $bi ,
'description' => $ad,
'extraImages' => $ei,
'group' => array('backgroundImage' => $gbi, 'description' => $gd, 'groupImage' => $gi, 'key' => $gk, 'shortTitle' => $gst, 'title' => $gt,),
'teacherReview' => $tr,
'rating' => $rating,
'shortTitle' => $st,
'tileImage' => $ti,
'title' => $title
);
//encode the new data
$content_json = json_encode($content);
file_put_contents( $savePath , $content_json );