繼上篇,因為朋友反應處理一千筆資料太慢的關系,所以在有空的時間就再寫了另一個方法,不過不知道是不是 php.ini 的限制,自機測試到 653 行就跳出了處理,出現記憶體不足,我想這可能跟設定有關系吧,至於另方面,其實更好的解決方法就是自動跳轉的處理方法,可是要寫很多東西呢 = =+
打亂資料並顯示 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| <?php class randData {
var $data_file = '';
function do_randDataId($data, $total, $saveRid = array()) { $rid = array_rand($data);
if ($total == 0) { $this->do_showData($saveRid); }else{ if (in_array($rid, $saveRid)) { $this->do_randDataId($data, $saveRid); }else{ $saveRid[] = $rid; unset($data[$rid]); $this->do_randDataId($data, count($data), $saveRid); } } }
function do_showData($rid_arr) { if (is_array($rid_arr)) { $data = file($this->data_file);
foreach($rid_arr as $k => $v) { list($id, $name, $passwd) = explode('<>', $data[$v]); echo "$id ==> $name<br />"; } } }
function show() { $data = file($this->data_file); $total= count($data);
$this->do_randDataId($data, $total); }
}
$o = new randData(); $o ->data_file = 'data.txt'; $o ->show(); ?>
|
生成 500 筆資料到 data.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <?php class writeData { var $data_file = '';
function show() {
$all_data = ''; $j = ''; for($i=0;$i<500;$i++) { $j++; $all_data .= "{$j}<>名稱<>密碼<>\n"; } $fp =fopen($this->data_file, 'w+'); fwrite($fp, $all_data); fclose($fp); }
}
$o = new writeData(); $o ->data_file = 'data.txt'; $o ->show(); ?>
|