前端之家收集整理的这篇文章主要介绍了
php实现根据字符串生成对应数组的方法,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_404_0@本文实例讲述了PHP实现根据字符串生成对应数组的方法,是比较实用的技巧。分享给大家供大家参考。具体方法如下:
@H_
404_0@先看看如下示例:
<div class="jb51code">
<pre class="brush:
PHP;">
<?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
);
?>
PHP;">
array(
'page' => array(
'index' => 'content','nav' => array(
array(
'image' => '1.jpg','name' => 'home'
),array(
'image' => '2.jpg','name' => 'about'
)
),'open' => true
)
)
);
?>
PHP;">
'content','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);
?>
_0@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
)
)
)
@H_