我可以在 PHP 中使用 json_decode() 之前从 json 中删除数据吗?

Can I remove data from json before using json_decode() in PHP?

我使用 curl 从 Web 服务获取大量数据。当我对该数据使用 json_decode() 时,我发现自己耗尽了内存限制。我知道我可以增加限制,但这不是一个好的解决方案,因为数据不断增加。

真正的问题是我只需要我正在获取的 json 的一小部分。所以,为了稍微简化一下,我的数据看起来像这样:

{ array [
  {           // object 1
  "field1": "xxx",
  "field2": "yyy",
  .
  .
  "field30": "zzz"
  },
  .
  .
  .           // object 15,000
]
}

现在数组[]中大约有15,000个对象,每个对象有30个字段。我预计未来几个月对象的数量将增长到 50,000 左右。

由于我需要所有对象但只需要字段 1 和 6,我想知道我是否可以以某种方式将上面的内容更改为更像这样的内容:

{ array [
  {             // object 1
  "field1": "xxx",
  "field6": "aaa"
  },
  .
  .
  .            // object 15,000
]
}

我想这会大大减少内存使用量。 有什么想法吗?

这解决了我的问题,尽管这可能不是所有情况下的解决方案。评论中的一些想法可能对 PHP 知识比我多的人有用。

我使用了@Igor 的想法(在评论中)。注意到我可以在 API 调用中添加开始日期(以毫秒为单位)和计数。每个对象的字段之一是以毫秒为单位的创建日期,因此我将计数设置为 1000,并将对象编号 1000 的创建日期设置为我的下一个开始日期。

像这样:

$lastTime = 0; // Use this as the startdate for my first API call
$i = 0; // Initialize counter to know when to stop the do-while loop
do {
  $i++;
  $api = 'domain.com/api?startdate=' . $lastTime . '&count=1000';
  // curl code here, returns $result
  foreach ($result as $obj) {
    // do whatever
    $lastTime = $obj->dateCreated; // This will give me the creation date for object no 1000, which I will use for my next API call.
  } 
}  while ($i >= 1000);