我在尝试向现有JSON对象添加新键值对时遇到问题。我尝试过使用array_merge,array_push,array_replace ......都没有成功。
所需格式(博客案例研究)
{
"1":{
"id":"1",
"name":"lew",
"email":"test@hotmail.co.uk",
"title":"lew",
"description":"lew",
"categories":[
"science"
],
"image":"no_image"
},
"2":{
"2",
"name":"lew",
"email":"test@hotmail.co.uk",
"title":"lew",
"description":"lew",
"categories":[
"science"
],
"image":"no_image"
}
}
这是我当前的代码(不是说如果JSON文件为空,我只需添加第一个元素,而不需要合并等。)
if ($data == null) {
$post_array = array('id' => "1",'name' => $_POST['name'], 'email' => $_POST['email'], 'title' => $_POST['title'], 'description' => $_POST['description'], 'categories' => $_POST['categories'], 'image' => $imageFilePath);
echo json_encode($post_array) . "<br>";
$blog_entry = array("1" => $post_array);
echo json_encode($blog_entry) . "<br>";
file_put_contents('blogPosts.json', json_encode($blog_entry));
} else {
$length_of_data = count($data);
$new_id = (string)($length_of_data + 1);
$post_array = array('id' => $new_id, 'name' => $_POST['name'], 'email' => $_POST['email'], 'title' => $_POST['title'], 'description' => $_POST['description'], 'categories' => $_POST['categories'], 'image' => $imageFilePath);
$merged = array_push($data, array($new_id => $post_array));
file_put_contents('blogPosts.json', json_encode($merged));
}
欢迎任何建议,谢谢
答案 0 :(得分:1)
此代码可以使用,并且希望您似乎想要!
(这只是一个解决方案,不是优雅甚至我会做的事情......)
<?php
$data = file_get_contents('blogPosts.json');
$data = (empty($data)) ? null : json_decode($data, true);
if ($data == null) {
$post_array = array('id' => "1", 'name' => $_POST['name'], 'email' => $_POST['email'], 'title' => $_POST['title'], 'description' => $_POST['description'], 'categories' => $_POST['categories'], 'image' => $imageFilePath);
$blog_entry = array("1" => $post_array);
file_put_contents('blogPosts.json', json_encode($blog_entry));
}
else {
$post_array = array('id' => $new_id, 'name' => $_POST['name'], 'email' => $_POST['email'], 'title' => $_POST['title'], 'description' => $_POST['description'], 'categories' => $_POST['categories'], 'image' => $imageFilePath);
$data[] = $post_array;
file_put_contents('blogPosts.json', json_encode($data));
}
var_dump($data);
这将完全相同,代码更少......
<?php
$data = file_get_contents('blogPosts.json');
$data = (empty($data)) ? array() : json_decode($data, true);
$new_id = count($data) + 1;
$post_array = array('id' => $new_id, 'name' => $_POST['name'], 'email' => $_POST['email'], 'title' => $_POST['title'], 'description' => $_POST['description'], 'categories' => $_POST['categories'], 'image' => $imageFilePath);
$data[] = $post_array;
file_put_contents('blogPosts.json', json_encode($data));
var_dump($data);
(编辑,有错误......)
答案 1 :(得分:0)
所以 - 你真的不需要&#34; key =&gt;值&#34;对,你只想将数组追加到你的主阵列。
在给定代码的情况下,最简单的方法就是这样做(注意:用此替换整个 if / else语句):
$new_id = count( (array)$data ) + 1;
$data[ $new_id ] = array('id' => $new_id, 'name' => $_POST['name'], 'email' => $_POST['email'], 'title' => $_POST['title'], 'description' => $_POST['description'], 'categories' => $_POST['categories'], 'image' => $imageFilePath);
file_put_contents( 'blogPosts.json', json_encode( $data ) );