php 递归多层关联数组
给你个思路,你结合具体项目改进吧,如下:
<?php/**
*?@author:?suifengtec?coolwp.com *?@date:2015-05-03?00:59:15 *?@last?Modified?by:suifengtec?coolwp.com *?@last?Modified?time:?2015-05-04?01:16:09 */function?coolwp_routes_to_array($_routes){
$routes?=?array();
foreach($_routes?as?$key=>$val){
if(is_array($val)){
$routes[$key]?=?$val;}else{
$routes[$key]?=?$val;
}
}
return?$routes;?
}
function?coolwp_get_route_from_array(array?$array,?$path,?$default?=?null){
$delimiter?=?'/';
if?(empty($path))?
throw?new?Exception('路径为空?');
$path?=?trim($path,?$delimiter);
$value?=?$array;
$parts?=?explode($delimiter,?$path);
foreach?($parts?as?$part)?{
if?(isset($value[$part]))?{
$value?=?$value[$part];
}?else?{
return?$default;
}
}
return?$value;
}
/*你的json*/
$string?=?file_get_contents("test.json");
$_routes?=?new?RecursiveIteratorIterator(
new?RecursiveArrayIterator(json_decode($string,?TRUE)),
RecursiveIteratorIterator::SELF_FIRST
);
/*==========以下为测试内容============*/
/*
转换为数组
*/$routes?=?coolwp_routes_to_array($_routes);
/*
获取具体的路由的值
*//*$movie_route?=?coolwp_get_route_from_array($routes?,'account/loginIndex.htm/movie');
//输出为:?电影
echo?$movie_route;
*/
print_r($routes);
祝愉快!