php递归处理多维数组

时间:2021-05-01 19:00:20
$a =array (
'menu' =>
array (
0 =>
array (
'id' => 1,
'name' => '舆情概况',
),
1 =>
array (
'id' => 2,
'name' => '舆情动态',
),
2 =>
array (
'id' => 3,
'name' => '舆情分析',
),
3 =>
array (
'id' => 4,
'name' => '舆情预警',
),
4 =>
array (
'id' => 5,
'name' => '舆情报告',
),
5 =>
array (
'id' => 6,
'name' => '微博舆情',
),
6 =>
array (
'id' => 10,
'name' => '我的收藏',
),
7 =>
array (
'id' => 11,
'name' => '话题追踪',
),
),
'config' =>
array (
0 =>
array (
'id' => 1001,
'name' => '菜单设置',
'url' => 'api/menu/menuList',
'_child' =>
array (
0 =>
array (
'id' => 1013,
'name' => '菜单列表',
'parent_id' => 1001,
'url' => 'api/menu/menuList',
),
1 =>
array (
'id' => 1014,
'name' => '添加/修改',
'parent_id' => 1001,
'url' => 'api/menu/menuSave',
),
2 =>
array (
'id' => 1031,
'name' => '菜单删除',
'parent_id' => 1001,
'url' => 'api/menu/menuDel',
),
3 =>
array (
'id' => 1015,
'name' => '模块管理',
'parent_id' => 1001,
'url' => 'api/model/menuList',
),
4 =>
array (
'id' => 1016,
'name' => '模块添加/修改',
'parent_id' => 1001,
'url' => 'api/model/menuSave',
),
),
),
1 =>
array (
'id' => 1002,
'name' => '关键词设置',
'url' => 'api/keyword/keywordList',
'_child' =>
array (
0 =>
array (
'id' => 1017,
'name' => '关键词类别设置',
'parent_id' => 1002,
'url' => 'api/keyword/classifyList',
),
1 =>
array (
'id' => 1018,
'name' => '主题词设置',
'parent_id' => 1002,
'url' => 'api/keyword/keywordList',
),
2 =>
array (
'id' => 1019,
'name' => '标签设置',
'parent_id' => 1002,
'url' => 'api/keyword/tagList',
),
),
),
2 =>
array (
'id' => 1003,
'name' => '报告设置',
'url' => 'api/report/reportList',
'_child' =>
array (
0 =>
array (
'id' => 1022,
'name' => '添加报告',
'parent_id' => 1003,
'url' => 'api/report/reportSave',
),
1 =>
array (
'id' => 1023,
'name' => '编辑报告',
'parent_id' => 1003,
'url' => 'api/report/reportSave',
),
2 =>
array (
'id' => 1032,
'name' => '报告删除',
'parent_id' => 1003,
'url' => 'api/report/reportDel',
),
),
),
3 =>
array (
'id' => 1004,
'name' => '预警设置',
'url' => 'api/warn/warnList',
'_child' =>
array (
0 =>
array (
'id' => 1024,
'name' => '添加预警',
'parent_id' => 1004,
'url' => 'api/warn/warnSave',
),
1 =>
array (
'id' => 1025,
'name' => '编辑预警',
'parent_id' => 1004,
'url' => 'api/warn/warnSave',
),
2 =>
array (
'id' => 1033,
'name' => '预警删除',
'parent_id' => 1004,
'url' => 'api/warn/warnDel',
),
),
),
4 =>
array (
'id' => 1005,
'name' => '角色管理',
'url' => 'api/role/roleList',
'_child' =>
array (
0 =>
array (
'id' => 1026,
'name' => '添加/修改角色',
'parent_id' => 1005,
'url' => 'api/role/roleUpAdd',
),
),
),
5 =>
array (
'id' => 1033,
'name' => '站点设置',
'url' => 'api/site/siteList',
),
6 =>
array (
'id' => 1006,
'name' => '用户管理',
'url' => 'api/user/userList',
'_child' =>
array (
0 =>
array (
'id' => 1028,
'name' => '添加用户',
'parent_id' => 1006,
'url' => 'api/user/userUpAdd',
),
1 =>
array (
'id' => 1029,
'name' => '编辑用户',
'parent_id' => 1006,
'url' => 'api/user/userUpAdd',
),
),
),
7 =>
array (
'id' => 1007,
'name' => '修改资料',
'url' => 'api/user/userModify',
),
8 =>
array (
'id' => 1008,
'name' => '操作日志',
'url' => 'api/log/logList',
),
9 =>
array (
'id' => 1009,
'name' => '任务中心',
'url' => 'api/schedule/scheduleList',
),
10 =>
array (
'id' => 1032,
'name' => '舆情待办',
),
11 =>
array (
'id' => 1030,
'name' => '回收站',
),
),
);



$b = array(
0 => '1',
1 => '2',
2 => '3',
3 => '4',
4 => '5',
5 => '6',
6 => '1001',
7 => '1013',
8 => '1014',
9 => '1002',
10 => '1003'
);

echo "<pre>";
//print_r($a);
print_r($b);
$c = recursive_array($a['config'],$b);
//print_r($c);

$d = recursive_array($a['menu'],$b);
//print_r($d);


function recursive_array($config,$b){

if( is_array($config) ){
foreach($config as $key=>$val){


if(is_array($val)){
if(in_array($val['id'], $b)){
$config[$key]['selected'] = 1;
}else{
$config[$key]['selected'] = 0;
}


if(isset($val['_child']) && is_array($val['_child'])){
$config[$key]['_child'] = recursive_array($val['_child'],$b);
}


}



}
}
return $config;


}

$new_a = [
'menu'=>$d,
'config'=>$c
];