1. 程式人生 > >PHP關於引用值傳遞的使用

PHP關於引用值傳遞的使用

需求:將返回資料中的amdin_uid替換為admin_uid對應的姓名 將許可權的key(permissions_key)替換為 許可權所對應的名稱

"data": [
            {
                "id": 5173,
                "admin_uid": 1,
                "org_id": 9,
                "permissions_key": "Admin/OrgAdmin/userlists",
                "request": "{\"page\":\"1\",\"size\":\"5\"}",
                "created_at": "2018-11-14 13:53:41"
            }
        ]

設計思路一:迴圈查詢admin_uid 所在的表查出對應的admin_name;迴圈查詢許可權的key(permissions_key) 所在的表查出對應的許可權名稱;

程式碼如下

$admin_name = array();
$admin = DB::table('org_admin_user')->get(['user_name','id']);
foreach ($admin as $ke=>$va){
    $admin_name[$va->id] = $va->user_name;
}
$permissions_arr = array();
$permissions = DB::table('org_permissions as a')
    ->join('org_permissions_translation as b','a.id','=','b.permissions_id')
    ->get(['b.permissions_name','a.permissions_key']);
foreach ($permissions as $kk=>$vv){
    $permissions_arr[$vv->permissions_key] = $vv->permissions_name;
}
foreach ($org_log['data'] as $key=>&$val){
    $val = (array)$val;
    $val['admin_name'] = $admin_name[$val['admin_uid']];
    if (array_key_exists($val['permissions_key'],$permissions_arr)){
        $val['permissions_name'] = $permissions_arr[$val['permissions_key']];
    }
    else{
        $val['permissions_name'] = '該許可權未匹配中文';
    }
}
return $org_log;