發表文章

目前顯示的是 12月, 2014的文章

[PHP] 將陣列中重複的值刪除最好方法 array_flip (移除陣列中重複的值)

使用 array_flip 比 array_unique 來的快速 參考連結: http://stackoverflow.com/questions/8321620/array-unique-vs-array-flip 上述網址提供的範例: http://codepad.org/AnpS69yw 用途: // 使用前的陣列 Array ( [0] => 8 [1] => 9 [2] => 10 [3] => 11 [4] => 10 [5] => 11 [6] => 8 [7] => 9 ) // 使用後的陣列 Array ( [6] => 8 [7] => 9 [4] => 10 [5] => 11 ) 底下為比較刪除重複值的方式有3種,array_flip快於 array_unique和array_keys 的測試 $test=array(); for($run=0; $run<1000; $run++) $test[]=rand(0,100); $time=microtime(true); for($run=0; $run<100; $run++) $out=array_unique($test); $time=microtime(true)-$time; echo 'Array Unique: '.$time."\n"; //----------------------------------------------- $time=microtime(true); for($run=0; $run<100; $run++) $out=array_keys(array_flip($test)); $time=microtime(true)-$time; echo 'Keys Flip: '.$time."\n"; //----------------------------------------------- $time=microtime(true); for($r

[PHP] 暫存檔 JSON, serialize or var_export?

文章 Cache a large array: JSON, serialize or var_export? 904 B array JSON Serialization var_export / include Length 105 150 151 Encoding 0.0000660419464111 0.00004696846008301 0.00014996528625488 Decoding 0.0011160373687744 0.00092697143554688 0.0010221004486084 18.07 kB array JSON Serialization var_export / include Length 1965 2790 3103 Encoding 0.0005040168762207 0.00035905838012695 0.001352071762085 Decoding 0.0017290115356445 0.0011298656463623 0.0056741237640381 290.59 kB array JSON Serialization var_export / include Length 31725 45030 58015 Encoding 0.0076849460601807 0.0057480335235596 0.02099609375 Decoding 0.014955997467041 0.010177850723267 0.030472993850708 4.54 MB array JSON Serialization var_export / include Length 507885 720870 1059487 Encoding 0.13873195648193 0.11841702461243 0.38376498222351 Decoding 0.29870986938477 0.21590781211853 0.538503170013