For example:
<?php
$config = Array (
' project|page|index ' => ' content ',
' Project|page|nav ' => Array (
Array (
' image ' => ' 1.jpg ', '
name ' => ' Home '
),
Array (
' image ' => ' 2.jpg ',
' name ' = > ' About '
)
,
' Project|page|open ' => true
);
? >
Generate the following array based on $config
<?php
$result = Array ('
project ' => Array ('
page ' => array ('
index ' => ' content '),
' Nav ' => Array (
' image ' => ' 1.jpg ',
' name ' => ' Home '
),
Array (
' image ' => ' 2.jpg ',
' name ' => ' about '
)
,
' open ' => true)
)
;
? >
This column more highlights: http://www.bianceng.cn/webkf/PHP/
Method: Using Eval to implement
<?php
$config = Array (
' project|page|index ' => ' content ',
' Project|page|nav ' => Array (
Array (
' image ' => ' 1.jpg ', '
name ' => ' Home '
),
Array (
' image ' => ' 2.jpg ',
' name ' = > ' About '
)
,
' Project|page|open ' => true
);
$result = Array ();
foreach ($config as $key => $val) {
$tmp = ';
$keys = Explode (' | ', $key);
For ($i =0, $len =count ($keys); $i < $len; $i + +) {
$tmp. = ' [']. $keys [$i].
if (Is_array ($val)) {
eval (' $result '. $tmp. ') = '. Var_export ($val, true). '; ');
ElseIf (is_string ($val)) {
eval (' $result '. $tmp. ') = '. $val. '; ');
else{
eval (' $result '. $tmp. = $val; ');
}
Print_r ($result);
? >
Output results:
Array
(
[Project] => Array ([
page] => array
([
index] => content
[nav] => Array
(
[0] => array
(
[image] => 1.jpg
[name] => home
)
[1] => array
(
[image] => 2.jpg
[name] => about
)
)
[Open] => 1
))