所以我有這個包含 45K 陣列的大檔案,我不能只在每個請求的高流量的實時服務器上打開一個大檔案,所以我使用array_chunk($array, 1000)并將它們保存在 46 個檔案中。
現在我想在訪問特定頁面時讀取這些檔案。
問題?
$offset似乎在某些頁面上作業正常,但主要抵消了- number(減號)的更改。檢查頁面25, 50, 75和更多...
我的數學有點(非常)弱,所以任何幫助將不勝感激。謝謝!
<?php
$page = ! empty( $_GET['page'] ) ? (int) $_GET['page'] : 1;
$limt = 40; //Page array/item limit
$fcnt = 46; //Total number of files
$tarr = 45187; //Total number of arrays
$fmu0 = (int)($limt*$fcnt*$page/$tarr); //Getting the file number according to page number
if(file_exists("$fmu0.json")){
$id = json_decode(file_get_contents("$fmu0.json"),true);
//Each file has 1000 arrays except the last file which has 187 arrays
$tpgs = ceil($tarr/$limt); //Total number of pages
$mult = $fmu0*count($id);
$offset = ($page - 1) * $limt-$mult;
if( $offset < 0 ){$offset = 0;}
$id = array_slice( $id, $offset, $limt );
var_dump($id);
}
?>
uj5u.com熱心網友回復:
每個檔案 1000 個物件和每頁 40 個物件使每個檔案 25 頁。
以下是查找包含$page數字物件的檔案的方法:
$fmu0 = floor($page / 25);
當第一頁為 1 時,如何$limt在該檔案中找到對應于number的 40 ( ) 個物件組的起始索引:$page
$offset = (($page - 1) * $limt) - ($fmu0 * 1000);
<?php
$page = (!empty($_GET['page']) && 0 < $_GET['page'])
? (int)$_GET['page']: 1;
$limt = 40; //Page array/item limit
// FIND FILE, 25 pages per file
$fmu0 = floor($page / 25);
if(file_exists("$fmu0.json")){
$id = json_decode(file_get_contents("$fmu0.json"),true);
// FIND GROUP of 40 ($limt) page objects, 1000 objects per file
$offset = (($page - 1) * $limt) - ($fmu0 * 1000);
$id = array_slice( $id, $offset, $limt );
var_dump($id);
}
?>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/524669.html
上一篇:重新計算縮放元素上的變換原點
