PHP:向JSON对象添加新的键值对
我在尝试向现有JSON对象添加新的键值对时遇到问题。我尝试过使用数组合并、数组推送、数组替换。。。一切都没有成功 所需格式(博客条目案例研究) 这是我当前的代码(不是说如果JSON文件是空的,我将简单地添加第一个元素,而不需要合并等)PHP:向JSON对象添加新的键值对,php,json,Php,Json,我在尝试向现有JSON对象添加新的键值对时遇到问题。我尝试过使用数组合并、数组推送、数组替换。。。一切都没有成功 所需格式(博客条目案例研究) 这是我当前的代码(不是说如果JSON文件是空的,我将简单地添加第一个元素,而不需要合并等) if($data==null){ $post\u array=array('id'=>“1”、'name'=>$post['name']、'email'=>$post['email']、'title'=>$post['title']、'description'=>
if($data==null){
$post\u array=array('id'=>“1”、'name'=>$post['name']、'email'=>$post['email']、'title'=>$post['title']、'description'=>$post['description']、'categories'=>$post['catories']、'image'=>$imageFilePath);
echo json_编码($post_数组)。“
”;
$blog\u entry=array(“1”=>$post\u array);
echo json_encode($blog_entry)。“
”;
文件内容('blogPosts.json',json_encode($blog_entry));
}否则{
$length\u of_data=计数($data);
$new_id=(字符串)($length_of_data+1);
$post\u array=array('id'=>$new\u id,'name'=>$post['name'],'email'=>$post['email'],'title'=>$post['title'],'description'=>$post['description'],'categories'=>$post['catories'],'image'=>$imageFilePath);
$merged=array\u push($data,array($new\u id=>$post\u array));
文件内容('blogPosts.json',json编码($merged));
}
欢迎您提出任何建议,谢谢您此代码将起作用,并且您似乎想要它 (这只是一个解决方案,不是优雅的,甚至不是我想做的事……)
因此-您实际上不需要“key=>value”对,您只需要将数组附加到主数组中
考虑到您的代码,最简单的方法是这样做(注意:用以下内容替换整个if/else语句):
请注意,这是无效的json,我只是为您格式化的。我们可以得到一个var\u dump($data)代码>没有任何额外的mod(在你的评论中你说json_encode-省去它,在合并行之前使用var_dump
或var_export
)'var_dump($data):array(1){[1]=>array(7){[“id”]=>string(1)“1”[“name”=>string(3)“lew”[“email”]=>string(28)”test@hotmail.co.uk“[“title”]=>字符串(3)“lew”[“description”=>string(3)“lew”[“categories”]=>array(1){[0]=>string(7)“science”}[“image”]=>string(8)“no_image”}
合并行后:array(2){[1]=>array(7){[“id”]=>string(1)“1”[“name”=>string(3)“lew”[“email”=>string(28)”test@hotmail.co.uk“[“title”]=>string(3)“lew”[“description”]=>string(3)“lew”[”类别“]=>array(1){[0]=>string(7)“science”}[“image”]=>string(8)“no_image”}[2]=>array(1){[2]=>array(7){[id”]=>string(1)“2”[“name”]=>string(4)“test”[“email”]=>string(15)”test@test.co.uk“[“title”]=>string(4)“test”[“description”]=>string(5)“tedst”[“categories”]=>array(1){[0]=>string(6)“health”}[“image”]=>string(8)“no_image”}}
似乎您只需要array\u push($data,$post\u array);
。id将自动分配。我很惊讶您在数组合并中没有成功,但我不知道哪里出了问题。感谢您花时间回复,此解决方案效果很好,@cale\u bJust以代码风格的观察方式更优雅地呈现:data==NULL
可能更适合表示为e> !data
,因为data==NULL
实际上与“data is falsey”相同,这是由于loose equal运算符。如果它确实在检查NULL,则应该是刚刚注意到的data==NULL
。如果在尝试添加之前JSON文件为空,则会松开键。(输出[{“id:“1”,“name:“lew”,“email:”test@hotmail.co.uk","标题“:”lew“,”描述“:”lew“,”类别“:[”运动“,”图像“:”无图像“}]
而不是{”id“:”1“,”名称“:”lew“,”电子邮件“:”test@hotmail.co.uk,“标题”:“lew”,"描述“:“lew”,“类别”:[“science“],“图像”:“无图像”age“}
对不起,我错误地复制了第二个示例。它是:{“1”:{“id”:“1”,“name”:“lew”,“email”:”test@hotmail.co.uk“,”标题":"卢“,“描述关于“:“卢”,“类别ies“:[“science“],”图像“:“没有年龄“}}
{
"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"
}
}
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));
}
<?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);
$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 ) );