title: How can I keep track of record number across multiple json objects in PHP
body: <p>I have an export of Customer Records that needed to be split up over several chunks of 500 records.  I grab each chunk through a REST request, save it to my server:</p>
  
  <pre><code>public function createImportFile($json)
  {
      $filePath = storage_path().'/import/'.$this-&gt;getImportFileName($this-&gt;import-&gt;chunkNumber);
      $importFile = fopen($filePath, 'w');
      $array = json_decode($json);
  
      fwrite($importFile, $json);
      fclose($importFile);
      return $filePath;
  
  }
  </code></pre>
  
  <p>Then after grabbing all of the chunks, I import all of the records.  I'm wondering what the best way would be to find the Nth record among all the chunks?</p>
  
  <p>Currently, I divide the record number that I'm looking for by the total number of chunks to find out which chunk the record will be in.  Then, I get the total records for the previous chunks and subtract this number from the record number to get the record's position in the chunk.</p>
  
  <pre><code>while ($this-&gt;recordNumber &lt;= $this-&gt;totalRecords) {
              $item = $this-&gt;getRecord($this-&gt;recordNumber);
              if (empty($item)) {
                  $this-&gt;recordNumber++;
                  continue;
              }
              $results = $this-&gt;translateItem($item);
              $this-&gt;recordNumber++;
   }
  public function getRecord($recordNumber)
  {
      if ($this-&gt;import-&gt;isChunkedImport()) {
          $chunkNumber = (integer) $this-&gt;returnChunkFromRecordNumber($recordNumber);
          $countInPrevChunks = intval($this-&gt;returnRecordCountForPrevChunks($chunkNumber));
          $chunkPosition = intval($this-&gt;getChunkPosition($recordNumber, $countInPrevChunks));
          $jsonObj = $this-&gt;getJsonObjectForChunkNumer($chunkNumber);
          return $jsonObj[$chunkPosition];
      } 
      else {
          $chunkPosition = $this-&gt;getChunkPosition($recordNumber, 0);
          $filePath = storage_path().'/import/'.$this-&gt;getImportFileName();
          return (array) json_decode(file_get_contents($filePath))[$chunkPosition];
      }
  }
  
  private function &amp;getJsonObjectForChunkNumer($chunkNumber)
  {
      if ($this-&gt;currentFileArray == null || ($chunkNumber != $this-&gt;lastChunkNumber)) {
          $filePath = storage_path().'/import/'.$this-&gt;getImportFileName($chunkNumber);
          $this-&gt;currentFileArray = json_decode(file_get_contents($filePath), true);
          $this-&gt;lastChunkNumber = $chunkNumber;
      }
      return $this-&gt;currentFileArray;
  }
  
  public function getChunkCount()
  {
      $filePath = storage_path().'/import/'.$this-&gt;getImportFileName();
      return count(json_decode(file_get_contents($filePath)));
  }
  
  public function returnChunkFromRecordNumber($recordNumber)
  {
  
      if ($recordNumber &gt;= $this-&gt;getChunkCount()) {
          if (is_int($recordNumber/$this-&gt;getChunkCount())) {
              if (($recordNumber/$this-&gt;getChunkCount()) == 1) {
                  return intval(1);
              }
              return intval(($recordNumber/$this-&gt;getChunkCount())-1);
          }
          else {
              return intval($recordNumber/$this-&gt;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-&gt;getChunkCount() * $chunkNumber;
  </code></pre>
  
  <p>I try to account for the first key for both Chunks and Records in the Chunks being 0, but I'm still missing the last record of the import.  It also seems like I might be making this more complicated than it needs to be.  I was wondering if anyone had advice or a more simple way to grab the Nth record.  I thought about possibly just numbering the records as I bring them in with the REST request, then I could find the Chunk containing the record number as an array key and then return that record:</p>
  
  <pre><code>public function createImportFile($json)
  {
      $filePath = storage_path().'/import/'.$this-&gt;getImportFileName($this-&gt;import-&gt;chunkNumber);
      $importFile = fopen($filePath, 'w');
      if ($this-&gt;import-&gt;chunkNumber == 0 &amp;&amp; $this-&gt;recordNumber == 0) $this-&gt;recordNumber = 1;
      $array = json_decode($json);
      $ordered_array = [];
      foreach ($array as $record) {
          $ordered_array[$this-&gt;recordNumber] = $record;
          $this-&gt;recordNumber++;
      }
      fwrite($importFile, json_encode($ordered_array));
      fclose($importFile);
      return $filePath;
  }
  </code></pre>
  
  <p>But I wasn't sure if that was the best approach.</p>