使用PHP从json文件中删除对象
嗯,我有一个web项目,我必须暂时保存东西,我开始使用json文件,到目前为止我可以添加和更新。 json文件如下所示:使用PHP从json文件中删除对象,php,json,Php,Json,嗯,我有一个web项目,我必须暂时保存东西,我开始使用json文件,到目前为止我可以添加和更新。 json文件如下所示: [ { "username": "Baldwin", "products": [ { "id": 0, "amount": 10 }, { "id": 1, "amount": 9 }, { "id": 2,
[
{
"username": "Baldwin",
"products": [
{
"id": 0,
"amount": 10
},
{
"id": 1,
"amount": 9
},
{
"id": 2,
"amount": 9
}
]
},
{
"username": "Alice",
"products": [
{
"id": 0,
"amount": 11
},
{
"id": 1,
"amount": 13
},
{
"id": 2,
"amount": 6
}
]
},
{
"username": "Terry",
"products": [
{
"id": 0,
"amount": 12
},
{
"id": 1,
"amount": 14
},
{
"id": 2,
"amount": 5
}
]
}
]
当我想删除一个特定的数组或者当我想完全删除它时,问题就出现了,我可以这样做,而且效果很好,但我怀疑为什么在删除对象时,会向json文件中添加其他字段,比如id
当我只删除“产品”数组中的一个产品时,会发生如下情况:
[
{
"username": "Baldwin",
"products": { "1": { "id": 1, "amount": 9 }, "2": { "id": 2, "amount": 9 } }
},
{
"username": "Alice",
"products": [
{ "id": 0, "amount": 11 },
{ "id": 1, "amount": 13 },
{ "id": 2, "amount": 6 }
]
},
{
"username": "Terry",
"products": [
{ "id": 0, "amount": 12 },
{ "id": 1, "amount": 14 },
{ "id": 2, "amount": 5 }
]
}
]
当我从json文件中删除一个完整的数组时,会发生如下情况:
{
"1": {
"username": "Alice",
"products": [
{ "id": 0, "amount": 11 },
{ "id": 1, "amount": 13 },
{ "id": 2, "amount": 6 }
]
},
"2": {
"username": "Terry",
"products": [
{ "id": 0, "amount": 12 },
{ "id": 1, "amount": 14 },
{ "id": 2, "amount": 5 }
]
}
}
要删除的我的php文件:
<?php
// load file
$data = file_get_contents('results.json');
// decode json to associative array
$json_arr = json_decode($data, true);
$flag = false;
// We check if the user wants to delete all or just one product
if(isset($_POST["all"])):
$username = $_POST["username"];
foreach ($json_arr as $key => $value):
// find the username on the json file
if($value["username"] == $username):
unset($json_arr[$key]);
break;
endif;
endforeach;
elseif(isset($_POST["one"])):
$username = $_POST["username"];
$id = $_POST["id"];
foreach ($json_arr as $key => $value):
// find the username on the json file
if($value["username"] == $username):
// loop products of the current username
foreach ($json_arr[$key]["products"] as $k => $product):
// find the id of the product
if($json_arr[$key]["products"][$k]["id"] == (int)$id):
// delete the product
unset($json_arr[$key]["products"][$k]);
endif;
endforeach;
endif;
endforeach;
endif;
// encode json and save to file
file_put_contents('results.json', json_encode($json_arr));
// redirect to show.php
header("Location: show.php");
?>
我一直在研究像这样的问题,但我找不到php的一些东西,我想知道如何解决这个问题,或者这是否正常。当使用unset($json_arr[0])时,会发生什么情况,第一个元素被删除,但键没有更新。如果在移除后检查数组,您会发现数组有两个元素,分别位于
$json\u arr[1]
和$json\u arr[2]
然后,当您对此执行json\u encode($json\u arr)
时,PHP的json解码器会查看数组,并且由于数组应该从0
第个元素开始,但该数组从1
开始,因此它决定为了保留键,该数组必须转换为一个关联数组,该数组将整数数组键转换为JSON中的字符串键
要获得简短快速的解决方案,您可以尝试:
$json_arr = array_diff($json_arr, [$key]);
你甚至可以使用
array\u splice
或array\u values
-作为灵感。我明白了,但我还是尝试了代码,这给我带来了一个错误:注意:数组到字符串的转换在第21行,这是我放置代码行的地方。我想说的是,也许你使用了$key
而不是[$key]
。但我很高兴它现在对你有用。在任何情况下,相关的代码行都应该被替换为$json\u arr=array\u diff($json\u arr,[$key])
和$json_arr=array_diff($json_arr[$key][“产品”],[$k])是的,我用了$key
而不是[$key]
,也解决了这个问题。谢谢