How to use the PHP array configuration file? & Nbsp; some configurations can be expressed as arrays (configuration parameter & gt; configuration value,), for example: & nbsp; array & nbsp; (& nbsp; var1 & gt; val1, & nbsp; & nbsp PHP array configuration file how to use?
Some configurations can be expressed as arrays ('configuration parameter '=> 'configuration value',), for example:
Array
(
'Var1' => 'val1 ',
'Var2' => 'val2 ',
'Var3' => 'val3 ',
);
How to use the configurations represented by these arrays?
First, add return to the front of the array and save it as a file separately:
Return array
(
'Var1' => 'val1 ',
'Var2' => 'val2 ',
'Var3' => 'val3 ',
);
Then, in another file, require it will return the array, and then use the extract function to convert the array to a variable.
Example:
$ Catid = 1;
// Returns the array
$ Var_array = require ('Category _ '. $ catid.'. php ');
// Print_r ($ var_array );
// Convert the array into a variable
Extract ($ var_array, EXTR_PREFIX_SAME, "new ");
// Real variable value
Echo $ catid;
Echo $ new_catid;
Echo $ module;
Echo $ catname;
?>
Category_1.php file:
Return array
(
'Catid' => '10 ',
'Module' => 'lightphp ',
'Type' => '1 ',
'Modelid' => '0 ',
'Catname' => 'website introduction ',
'Description' => '',
);
?>
-------------------------------------------------
References:
PHP extract
Definition and usage
The PHP extract () function imports variables from the array to the current symbol table.
For each element in the array, the key name is used for the variable name, and the key value is used for the variable value.
The second parameter type is used to specify how the extract () function treats such conflicts when a variable already exists and an element with the same name exists in the array.
This function returns the number of successfully set variables.
Syntax
Extract (array, extract_rules, prefix)
Example
$ A = 'original ';
$ My_array = array ("a" => "Cat", "B" => "Dog", "c" => "Horse ");
Extract ($ my_array );
Echo "\ $ a = $ a; \ $ B = $ B; \ $ c = $ c ";
?>
Output:
$ A = Cat;
$ B = Dog;
$ C = Horse
Example 2
Convert the array key name to a variable and output
// Assume $ var_array is the array returned by wddx_deserialize.
$ Size = "large ";
$ Var_array = array ("color" => "blue ",
"Size" => "medium ",
"Shape" => "sphere ");
Extract ($ var_array, EXTR_PREFIX_SAME, "wddx ");
Echo "$ color, $ size, $ shape, $ wddx_sizen ";
?>
The above example will output:
Blue, large, sphere, medium
$ Size is not overwritten. because EXTR_PREFIX_SAME is specified, $ wddx_size is created.
If EXTR_SKIP is specified, $ wddx_size is not created.
EXTR_OVERWRITE will set the value of $ size to "medium ",
EXTR_PREFIX_ALL creates a new variable $ wddx_color, $ wddx_size, and $ wddx_shape.
Zhang Qing (mesh), Weibo: http://t.qq.com/zhangking
From mesh horizon: http://blog.why100000.com
Why 100,000 computer learning networks: http://www.why100000.com
2013-4-2