php - Convert array of strings delimited to array of key & values -


i have example of array:

$roles = [     "dashboard.read" => true,     "dashboard.section1.read" => true,     "members.member.create" => false,     "members.member.read" => true,     "members.member.view.update" => true,     "members.member.view.section2.delete" => false,     "members.member.view" => true ]; 

and need method convert arrays of similar structure result :

$roles = [     "dashboard" => [         "read" => true,         "section1" => [             "read" => true         ]     ],     "members" => [         "member" => [             "create" => false,             "read" => true,             "view" => [                 "update" => true,                 "section2" => [                     "delete" => false                 ],                 true,             ],         ]     ] ]; 

i can't figure out how this.

i took challenge solve question, here go, have added details right code:

// defined roles, stated above $roles = [     "dashboard.read" => true,     "dashboard.section1.read" => true,     "members.member.create" => false,     "members.member.read" => true,     "members.member.view.update" => true,     "members.member.view.section2.delete" => false,     "members.member.view" => true ];  // magic array, have results later $magic = array();  // iterate trough roles foreach($roles $key => $value) {     // split roles , reverse array, can start setting value     $rolenamepart = array_reverse(explode(".", $key));      // array used recursive create array in     $magiccreationarray = array();      // iterate trough role name parts starting @ "bottom"     foreach($rolenamepart $keypart) {          // if empty, bottom , can set value right there         if(empty($magiccreationarray)) {             $magiccreationarray[$keypart] = $value;          // if not, must inbetween, go 1 level up. needed create temporary value this, guess         } else {             $tmp = $magiccreationarray;             unset($magiccreationarray);             $magiccreationarray[$keypart] = $tmp;         }     }     // merge data magic array, use array_merge_recursive not override example "dashboard"      $magic = array_merge_recursive($magic, $magiccreationarray); }  // here go fam print_r($magic); 

hope it.

nice 1 read: http://php.net/manual/en/function.array-merge-recursive.php


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -