简体   繁体   中英

How to convert this array to fuelphp acl format

Hi I'm a newbie of FuelPHP. I'm makin' a demo use ACL. I have fetched all roles from database with format as the code below

$data = array(
  array('admin'=>array(
    'none' => array(
        'crudform' => array('create','index')
      )  
  )),
  array('admin'=>array(
    'none' => array(
        'cruddept' => array('create','view')
      )  
  )),
);

And now I want to convert that array to format as

$data = array(
  'admin' => array(
    'none'=>array(
      'crudform' => array(
         'create',
         'index'
       ),
       'cruddept'=>array(
          'create',
          'view'
       )
     )
   )
)

How I can do that ?

After you retreive your data from database, you can convert your array using a function just like this one

function change_array($data)
{
    $result = array('admin' => array('none' => array()));
    foreach ($data as $key => $value)
        foreach ($data[$key]['admin']['none'] as $key_child => $value_child)
            $result['admin']['none'][$key_child] = $value_child;
    return $result;
}

All you need to do, is to use it like this

$data = change_array($data);

Thanks Mr Khalid, your idea has provide a way for me how to resolve my solution. I have found the way to build array as I want. This is my code

function build_role_array($roles){
        $final = array();
        foreach($roles as $row){
            foreach($row as $role=>$value){
                if(!isset($final[$role])){
                    $final[$role] = array();
                }
                foreach($value as $module=>$area){
                    if(!isset($final[$role][$module])){
                        $final[$role][$module] = array();
                    }
                    foreach($area as $controller=>$rights){
                        $final[$role][$module][$controller] = $rights;
                    }
                }
            }
        }
        return $final;
    }

Thanks for support

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM