这次的代码支持无唯一父级(上次的)的一维数组转多层级从属结构。
代码实现
if(!function_exists('deal_list_to_tree2')){
/**
* 方法 deal_list_to_tree2,一维数组根据$parent_id的值转为多维数组
*
* @param array $data 待处理的一维数组
* @param string $pkName 用于转化为多维数组的主键字段
* @param string $pIdName 用于转化为多维数组的字段(根据该字段值转换)
* @param string $childName 子级的字段名
* @param bool $is_empty_childrens 是否返回空的子数组(childrens[])(true:是,false:否)
* @param string $rootId 根节点$pkName值
*
* @return array $new_data 返回处理好的(多层级)多维数组
*
*/
function deal_list_to_tree2($data, $pkName='id', $pIdName='parent_id', $childName='children_list', $is_empty_childrens=false, $rootId=''){
$new_data = [];
if(!empty($data)){
foreach($data as $sorData){
if(array_key_exists($childName, $sorData) && !empty($sorData[$childName])){
$res = deal_list_to_tree2($data, $pkName, $pIdName, $childName, $is_empty_childrens, $sorData[$pkName]);
}else{
if($sorData[$pIdName] == $rootId){
if($sorData[$pkName] != $rootId){
$res = deal_list_to_tree2($data, $pkName, $pIdName, $childName, $is_empty_childrens, $sorData[$pkName]);
}
if(!empty($res) && !$is_empty_childrens){
if(array_key_exists($childName, $sorData)) {
if(array_key_exists($childName, $sorData)){
for($i=0; $i < count($res); $i++){
$sorData[$childName][] = $res[$i];
}
}else{
$sorData[$childName][] = $res;
}
}else{
$sorData[$childName] = $res;
}
}
$new_data[] = $sorData;
}
}
}
}
return $new_data;
}
}
实现效果
{
"code": 0,
"msg": "SUCCESS",
"action": "",
"data": {
"list": [
{
"object_id": "407",
"object_name": "工作中心",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "workCenter",
"object_sort": "0",
"is_dir": "1",
"p_object_no": "",
"order_no": "0",
"is_menu": "1",
"is_priv": "1",
"is_flow": "1",
"is_samp": "1",
"system_no": "asset",
"children_list": [
{
"object_id": "408",
"object_name": "资产管理",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "assetManage",
"object_sort": "0",
"is_dir": "1",
"p_object_no": "workCenter",
"order_no": "0",
"is_menu": "1",
"is_priv": "1",
"is_flow": "1",
"is_samp": "1",
"system_no": "asset",
"children_list": [
{
"object_id": "409",
"object_name": "资产信息",
"object_module": "assetList",
"object_method": "asset_info",
"object_para": "",
"object_no": "asset-info-page",
"object_sort": "1",
"is_dir": "0",
"p_object_no": "assetManage",
"order_no": "0",
"is_menu": "1",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset",
"children_list": [
{
"object_id": "530",
"object_name": "采购按钮",
"object_module": "",
"object_method": "para",
"object_para": "",
"object_no": "asset-purchase-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "asset-info-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "531",
"object_name": "入库按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "warehouse-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "asset-info-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "532",
"object_name": "资产调拨按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "asset-allot-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "asset-info-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "533",
"object_name": "资产转让按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "asset-transfer-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "asset-info-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "0",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "534",
"object_name": "资产报废按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "asset-discard-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "asset-info-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "0",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "535",
"object_name": "资产批量入库按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "asset-batch-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "asset-info-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "0",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
}
]
},
{
"object_id": "540",
"object_name": "资产详情页面",
"object_module": "assetDetail",
"object_method": "",
"object_para": "",
"object_no": "asset-detail-page",
"object_sort": "1",
"is_dir": "0",
"p_object_no": "assetManage",
"order_no": "0",
"is_menu": "1",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset",
"children_list": [
{
"object_id": "541",
"object_name": "资产调拨按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "detail-allot-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "asset-detail-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "542",
"object_name": "资产转让按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "detail-transfer-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "asset-detail-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "543",
"object_name": "资产报废按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "detail-discard-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "asset-detail-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
}
]
}
]
},
{
"object_id": "410",
"object_name": "配置管理",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "setting-manage",
"object_sort": "0",
"is_dir": "1",
"p_object_no": "workCenter",
"order_no": "0",
"is_menu": "1",
"is_priv": "1",
"is_flow": "1",
"is_samp": "1",
"system_no": "asset",
"children_list": [
{
"object_id": "411",
"object_name": "类别管理",
"object_module": "classifyMgt",
"object_method": "",
"object_para": "",
"object_no": "classifyMgt",
"object_sort": "1",
"is_dir": "0",
"p_object_no": "setting-manage",
"order_no": "0",
"is_menu": "1",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset",
"children_list": [
{
"object_id": "536",
"object_name": "新增资产大类",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "create-class-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "classifyMgt",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "537",
"object_name": "表格编辑按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "table-edit-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "classifyMgt",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "538",
"object_name": "表格删除按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "table-delete-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "classifyMgt",
"order_no": "0",
"is_menu": "0",
"is_priv": "0",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "539",
"object_name": "新增小类按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "create-type-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "classifyMgt",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
}
]
}
]
},
{
"object_id": "414",
"object_name": "物料信息",
"object_module": "MatterList",
"object_method": "",
"object_para": "",
"object_no": "MatterList",
"object_sort": "1",
"is_dir": "0",
"p_object_no": "workCenter",
"order_no": "0",
"is_menu": "1",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset",
"children_list": [
{
"object_id": "547",
"object_name": "物料入库按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "material-warehouse-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "MatterList",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "548",
"object_name": "物料调拨按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "material-allot-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "MatterList",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "549",
"object_name": "物料批量导入按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "material-batch-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "MatterList",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
}
]
},
{
"object_id": "416",
"object_name": "资产盘点",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "asset-inventory",
"object_sort": "0",
"is_dir": "1",
"p_object_no": "workCenter",
"order_no": "0",
"is_menu": "1",
"is_priv": "1",
"is_flow": "1",
"is_samp": "1",
"system_no": "asset",
"children_list": [
{
"object_id": "417",
"object_name": "资产盘点",
"object_module": "assetInventory",
"object_method": "",
"object_para": "",
"object_no": "asset-inventory-page",
"object_sort": "1",
"is_dir": "0",
"p_object_no": "asset-inventory",
"order_no": "0",
"is_menu": "1",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset",
"children_list": [
{
"object_id": "544",
"object_name": "新增盘点按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "create-inventory-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "asset-inventory-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "545",
"object_name": "盘点编辑按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "inventory-edit-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "asset-inventory-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "546",
"object_name": "盘点删除按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "inventory-delete-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "asset-inventory-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
}
]
}
]
},
{
"object_id": "418",
"object_name": "统计报表",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "static-report",
"object_sort": "1",
"is_dir": "0",
"p_object_no": "workCenter",
"order_no": "0",
"is_menu": "1",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "421",
"object_name": "申请单管理",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "ApplicationFormMgt",
"object_sort": "1",
"is_dir": "0",
"p_object_no": "workCenter",
"order_no": "0",
"is_menu": "1",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "426",
"object_name": "组织架构",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "organization-framework-page",
"object_sort": "1",
"is_dir": "0",
"p_object_no": "workCenter",
"order_no": "0",
"is_menu": "1",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "428",
"object_name": "权限管理",
"object_module": "permissionManage",
"object_method": "",
"object_para": "",
"object_no": "permission-manage-page",
"object_sort": "1",
"is_dir": "0",
"p_object_no": "workCenter",
"order_no": "0",
"is_menu": "1",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset",
"children_list": [
{
"object_id": "554",
"object_name": "新增主权限项按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "create-main-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "permission-manage-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "555",
"object_name": "编辑权限按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "edit-permission-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "permission-manage-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "556",
"object_name": "添加子权限项按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "create-child-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "permission-manage-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "557",
"object_name": "删除权限按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "delete-permission-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "permission-manage-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
}
]
},
{
"object_id": "429",
"object_name": "菜单管理",
"object_module": "menuManage",
"object_method": "",
"object_para": "",
"object_no": "menu-manage-page",
"object_sort": "1",
"is_dir": "0",
"p_object_no": "workCenter",
"order_no": "0",
"is_menu": "1",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset",
"children_list": [
{
"object_id": "560",
"object_name": "新增菜单按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "create-menu-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "menu-manage-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "561",
"object_name": "编辑菜单按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "edit-menu-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "menu-manage-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "562",
"object_name": "删除菜单按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "delete-menu-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "menu-manage-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
}
]
},
{
"object_id": "431",
"object_name": "权限模板",
"object_module": "permissionTemplate",
"object_method": "",
"object_para": "",
"object_no": "permission-template-page",
"object_sort": "1",
"is_dir": "0",
"p_object_no": "workCenter",
"order_no": "0",
"is_menu": "1",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset",
"children_list": [
{
"object_id": "563",
"object_name": "新增权限模板按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "create-template-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "permission-template-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "564",
"object_name": "编辑权限模板按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "edit-template-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "permission-template-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "565",
"object_name": "分配权限按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "assign-permission-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "permission-template-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "566",
"object_name": "删除权限模板按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "delete-template-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "permission-template-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
}
]
},
{
"object_id": "518",
"object_name": "账号列表",
"object_module": "accountList",
"object_method": "",
"object_para": "",
"object_no": "account-list",
"object_sort": "1",
"is_dir": "0",
"p_object_no": "workCenter",
"order_no": "0",
"is_menu": "1",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset",
"children_list": [
{
"object_id": "567",
"object_name": "新增账号按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "create-account-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "account-list",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "568",
"object_name": "批量导入按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "batch-import-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "account-list",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "569",
"object_name": "编辑账号按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "edit-account-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "account-list",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "570",
"object_name": "冻结账号按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "freeze-account-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "account-list",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
}
]
},
{
"object_id": "550",
"object_name": "物料详情页",
"object_module": "MatterDetailed",
"object_method": "",
"object_para": "",
"object_no": "material-detail-page",
"object_sort": "1",
"is_dir": "0",
"p_object_no": "workCenter",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset",
"children_list": [
{
"object_id": "551",
"object_name": "新增损耗按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "create-waste-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "material-detail-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "552",
"object_name": "新增闲置按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "create-idle-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "material-detail-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
},
{
"object_id": "553",
"object_name": "新增在用按钮",
"object_module": "",
"object_method": "",
"object_para": "",
"object_no": "create-using-button",
"object_sort": "3",
"is_dir": "0",
"p_object_no": "material-detail-page",
"order_no": "0",
"is_menu": "0",
"is_priv": "1",
"is_flow": "0",
"is_samp": "0",
"system_no": "asset"
}
]
}
]
}
]
}
}