Php 从json文件中删除字段(“ID”):
试图从json文件中删除字段和值(“ID”): 有人能帮忙吗 JSON: 我试过这个:Php 从json文件中删除字段(“ID”):,php,json,Php,Json,试图从json文件中删除字段和值(“ID”): 有人能帮忙吗 JSON: 我试过这个: <?php $file = file_get_contents("uscf.json"); $file = str_replace('"ID": "[0-9][0-9][0-9][0-9]",','',$file); $array = json_decode($file, true); unset ($array.ID); echo $array; ?> 为了不改变原始数据,我会将原始
<?php
$file = file_get_contents("uscf.json");
$file = str_replace('"ID": "[0-9][0-9][0-9][0-9]",','',$file);
$array = json_decode($file, true);
unset ($array.ID);
echo $array;
?>
为了不改变原始数据,我会将原始数组映射到一个新数组,并通过删除
ID
键
$array=json\u decode(file\u get\u contents(“uscf.json”),true);
$filter=['ID'=>true];//只有钥匙才重要
$newArray=array\u映射(函数($item)使用($filter){
返回数组差异键($item,$filter);
}美元阵列);
Demo ~实际上,在数据解码后处理数据总是比直接操作JSON更容易 解码文件中的JSON,从所有数组中删除
ID
元素,然后将更新数组的JSON写回文件
$array = json_decode(file_get_contents("uscf.json"), true);
foreach ($array as &$el) {
unset($el['ID']);
}
file_put_contents("uscf.json", json_encode($array));
您标记了
javascript
,但是您在PHP中尝试了这个。。。?哪种语言?最终结果应该是什么样的?您在问题中提到了“ID”:“16733310”
,但您的PHP代码尝试似乎希望使用正则表达式删除所有具有4位值的ID
属性您是对的。我的错误。很抱歉我有点累了。页面有一个错误。期望的结果是“ID”:“16733310”和“ID”:“16429901”应该消失。全部300人。
[{
"Rating": "1600",
"Name": "LARRATEGUI,MARTIN",
"Expires": "1000.10.10"
},{
"Rating": "1353",
"Name": "ADDABBO,ERIC M",
"Expires": "1000.10.10"
}]
$array = json_decode(file_get_contents("uscf.json"), true);
foreach ($array as &$el) {
unset($el['ID']);
}
file_put_contents("uscf.json", json_encode($array));