php - 使用 PHP 从 .json 文件中删除 JSON 记录

标签 php json

我有一个像这样的 json 文件:

[["{\"id\":1474721566304,\"name\":\"GGG\",\"brand\":\"GG\",\"price\":\"3\"}"],["{\"id\":1474721570904,\"name\":\"GGGH\",\"brand\":\"GGH\",\"price\":\"4\"}"],["{\"id\":1474721574188,\"name\":\"GGGH\",\"brand\":\"GGHH\",\"price\":\"5\"}"]]

我想做的是通过它的 id 从中删除一条记录。为此,我有以下 PHP 代码:

<?php
$string = file_get_contents("products.json");
$json_a = json_decode($string, true); //turning JSON-string into an array containing JSON-strings

$arr = array();
foreach ($json_a as $key) {
    array_push($arr,json_decode($key[0],true)); //and here you turning each of the JSON-strings into objects themselves


}
$data= $_GET['data'];
$i=0;
foreach($arr as $element) {
   if($data == $element["id"]){
      unset($arr[$i]);//removing the product by ID
   }
   $i++;
}

var_dump($arr);
$arr2 = array();

foreach ($arr as $key) {//trying to make it look like the original json.
   array_push($arr2,json_decode($key[0],true));
}
//var_dump($arr2);
echo json_encode($arr2);
?>

我从这段代码中得到的是:

array(2) { [0]=> array(4) { ["id"]=> float(1474721566304) ["name"]=> string(3) "GGG" ["brand"]=> string(2) "GG" ["price"]=> string(1) "3" } [1]=> array(4) { ["id"]=> float(1474721570904) ["name"]=> string(4) "GGGH" ["brand"]=> string(3) "GGH" ["price"]=> string(1) "4" } }

我真的不知道如何让这个数组看起来像我在这篇文章中首先显示的原始 JSON。我尝试了很多不同的东西,但我无法让它发挥作用。我的想法是在通过 ID 删除记录后,用我在这里尝试构建的新 JSON 替换旧 JSON。

我是 php 的新手,非常感谢任何关于我的问题的意见。

最佳答案

首先 - 您输入的 JSON 是错误的:

array (size=3)
  0 => 
    array (size=1)
      0 => string '{"id":1474721566304,"name":"GGG","brand":"GG","price":"3"}' (length=58)
  1 => 
    array (size=1)
      0 => string '{"id":1474721570904,"name":"GGGH","brand":"GGH","price":"4"}' (length=60)
  2 => 
    array (size=1)
      0 => string '{"id":1474721574188,"name":"GGGH","brand":"GGHH","price":"5"}' (length=61)

您没有“id”、“name”、“GGG”和其他键。你只有一根长绳子。您应该删除不必要的引号。之后您的 JSON 应该如下所示:

[[{"id":1474721566304,"name":"GGG","brand":"GG","price":"3"}],[{"id":1474721570904,"name":"GGGH","brand":"GGH","price":"4"}],[{"id":1474721574188,"name":"GGGH","brand":"GGHH","price":"5"}]]

最后,您的 PHP 代码可以更短:

$json = "[[{\"id\":1474721566304,\"name\":\"GGG\",\"brand\":\"GG\",\"price\":\"3\"}],[{\"id\":1474721570904,\"name\":\"GGGH\",\"brand\":\"GGH\",\"price\":\"4\"}],[{\"id\":1474721574188,\"name\":\"GGGH\",\"brand\":\"GGHH\",\"price\":\"5\"}]]";
$input = json_decode($json, true);
$output = array();
foreach($input as $element) { //you don't need to declare yet another array, just use the one you already have
    if($_GET['data'] != $element[0]["id"]){ //and not unset, just add to new array if you want
       $output[] = $element; //shorter and faster than array_push()
    }
}
echo json_encode($output);

关于php - 使用 PHP 从 .json 文件中删除 JSON 记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39679944/

相关文章:

php - textarea 偶尔会在 $_POST 中丢失,但 content_length 总是正确的?

php - 获取 <select> 中 <option> 的内容而不是值

php读取产品数据txt文件

php - 表单提交后从数据库中获取最后一条记录的id

java - JSON 数据未加载回 ArrayList

java - 需要一个字符串,但在第 1 行第 29 列路径处为 BEGIN_OBJECT

json - 类型 'Object' 的参数不可分配给类型 'JSON' Httpclient GET 的参数

json - 使用 Swift 4 Decodable 解析具有混合属性值的 JSON

php - 根据magento中的过滤器设置分页

javascript - 为 NodeJS 转换 api json 响应