Generating a PHP array file

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.