1. One of the beauty of interpretive languages is the ability to dynamically generate code and invoke execution ~
2. For a small amount of data (thousands of bar?) ) (Key,value), save the array file, perform the lookup operation, the efficiency should be better than the database operation;
3. PHP array, is a hash array, by default, key is 0, 1, 2, ...
By the way, these are the lack of fun work, even though I feel ";" Representing the end of a statement is justified;
Not much to say, the code:
To generate an array file:
|
If(! File_exists($srcFilePath)){
return False; }
$Info= Array();
$srcInfo=File($srcFilePath);
If(Empty($srcInfo)){
return False; }
Foreach($srcInfo as $line){
$line= Trim($line);
If(Empty($line)){
Continue; }
List($key, $Value)= Explode(‘ ‘, $line);
$cityInfo[$citycode]= $city; }
$Content="<?php\n"; $Content.="\ $myinfo =". Var_export($Info, true) . "; \ r \ n"; $Content . = "?>";
File_put_contents($cacheFilePath, $content); |
Find
|
/* Find value by key */
Include($cacheFilePath);
If(Empty( $myinfo ) ) {
return false;
if ( $myinfo [ $key ]) ) {
return $myinfo [ $key ]
return "
/* Find key by Value * /
Include($cacheFilePath);
If (Empty($myinfo) ) {
return False; }
foreach ($myinfo as $key = = $value) { if ($value = = $search _value) { return $key;
}
}
|
Generating a PHP array file