我如何跟踪多个json对象中的记录编号在PHP中
问题描述:
我有一个导出的客户记录需要分成几个大块的500个记录。我抓住通过REST请求的每个块,将其保存到我的服务器:我如何跟踪多个json对象中的记录编号在PHP中
public function createImportFile($json)
{
$filePath = storage_path().'/import/'.$this->getImportFileName($this->import->chunkNumber);
$importFile = fopen($filePath, 'w');
$array = json_decode($json);
fwrite($importFile, $json);
fclose($importFile);
return $filePath;
}
然后抓住所有块后,我导入所有的记录。我想知道在所有块中找到第N条记录的最佳方法是什么?
目前,我将我正在查找的记录数除以组块的总数,以找出记录将在哪个块中。然后,我得到前面块的总记录并从中减去此数记录号以获取记录在块中的位置。
while ($this->recordNumber <= $this->totalRecords) {
$item = $this->getRecord($this->recordNumber);
if (empty($item)) {
$this->recordNumber++;
continue;
}
$results = $this->translateItem($item);
$this->recordNumber++;
}
public function getRecord($recordNumber)
{
if ($this->import->isChunkedImport()) {
$chunkNumber = (integer) $this->returnChunkFromRecordNumber($recordNumber);
$countInPrevChunks = intval($this->returnRecordCountForPrevChunks($chunkNumber));
$chunkPosition = intval($this->getChunkPosition($recordNumber, $countInPrevChunks));
$jsonObj = $this->getJsonObjectForChunkNumer($chunkNumber);
return $jsonObj[$chunkPosition];
}
else {
$chunkPosition = $this->getChunkPosition($recordNumber, 0);
$filePath = storage_path().'/import/'.$this->getImportFileName();
return (array) json_decode(file_get_contents($filePath))[$chunkPosition];
}
}
private function &getJsonObjectForChunkNumer($chunkNumber)
{
if ($this->currentFileArray == null || ($chunkNumber != $this->lastChunkNumber)) {
$filePath = storage_path().'/import/'.$this->getImportFileName($chunkNumber);
$this->currentFileArray = json_decode(file_get_contents($filePath), true);
$this->lastChunkNumber = $chunkNumber;
}
return $this->currentFileArray;
}
public function getChunkCount()
{
$filePath = storage_path().'/import/'.$this->getImportFileName();
return count(json_decode(file_get_contents($filePath)));
}
public function returnChunkFromRecordNumber($recordNumber)
{
if ($recordNumber >= $this->getChunkCount()) {
if (is_int($recordNumber/$this->getChunkCount())) {
if (($recordNumber/$this->getChunkCount()) == 1) {
return intval(1);
}
return intval(($recordNumber/$this->getChunkCount())-1);
}
else {
return intval($recordNumber/$this->getChunkCount());
}
}
else {
return intval(0);
}
}
public function getChunkPosition($recordNumber, $countInPrevChunks)
{
$positionInChunk = $recordNumber - $countInPrevChunks;
if ($positionInChunk == 0) {
return $positionInChunk;
}
return $positionInChunk - 1;
}
public function returnRecordCountForPrevChunks($chunkNumber)
{
if ($chunkNumber == 0) {
return 0;
}
else {
return $this->getChunkCount() * $chunkNumber;
我试图解释在大块为0这两个块与记录的第一个关键,但我仍然失踪进口的最后一个记录。我似乎也许会让这个过程比需要的更复杂。我想知道是否有人有建议或更简单的方法来获得第N张唱片。我想过可能只是编号的记录,因为我为他们带来与REST请求,然后我可以找到包含的记录数为数组键的组块,然后返回该记录:
public function createImportFile($json)
{
$filePath = storage_path().'/import/'.$this->getImportFileName($this->import->chunkNumber);
$importFile = fopen($filePath, 'w');
if ($this->import->chunkNumber == 0 && $this->recordNumber == 0) $this->recordNumber = 1;
$array = json_decode($json);
$ordered_array = [];
foreach ($array as $record) {
$ordered_array[$this->recordNumber] = $record;
$this->recordNumber++;
}
fwrite($importFile, json_encode($ordered_array));
fclose($importFile);
return $filePath;
}
但我不当然,如果这是最好的方法。
答
有了大量的记录,您可以使用数据库表。 MySQL可以轻松处理数以万计的记录。你甚至不需要存储整个记录。也许只是:
record_no | chunk_no | position_in_chunk
-
record_no
:主键。唯一标识符此记录 -
chunk_no
:哪个块包含记录 -
position_in_chunk
:凡块内是位于
放一个UNIQUE(chunk_no, position_in_chunk)
指数表中的记录。
然后当您拉动记录时,为它们分配一个数字,建立数据库表,并在您将记录写入磁盘时保存表。未来,要获得特定的记录,您需要的只是它的编号。
如果您不想使用数据库,您还可以将此数据存储为JSON文件,但检索性能会因无法每次打开并解析大型JSON文件而受到影响。