4

I want to create hierarchical Enumarations for my PHP Applications and thought of things like

abstract class User_Roles extends Enum {

    const Administrator = "";
    const Account_Manager = "Administrator";
    const BlogAuthor = "Account_Manager";
    const CommentManager = "Account_Manager";

}

I'm using this Enum-Class: SO

So each child has his parent Node as it's value.

This is how I would do this:

    $constants = $reflector->getConstants();
    foreach ($constants as $key => $value) {
        if ($value == "") {
           $returnHierarchy[$key] = array();
           continue;
        }
        $returnHierarchy[][$value] = $key;
    }

But I have some issues with the multidimensional array which I want to create.

So it should look like this:

[Administrator]
{
    [Account_Manager]
    {
        [BlogAuthor]
        [CommentManager]
    }
}

But I end up in stuff like this:

array(4) (
  [Administrator] => array(0)
  [0] => array(1) (
    [Administrator] => (string) Account_Manager
  )
  [1] => array(1) (
    [Account_Manager] => (string) BlogAuthor
  )
  [2] => array(1) (
    [Account_Manager] => (string) CommentManager
  )
)

Is there anything that I misunderstand or overlook?

Community
  • 1
  • 1
Snickbrack
  • 1,253
  • 4
  • 21
  • 56

1 Answers1

1

You have to loop over $contstants and search inside your $returnHierarchy if your $value role already there append if no create new one

To search in array you have to use recursive function which will return matched sub array index reference

// IMPORTANT: it return reference function mae mast start with &
function &searchKeyInArray($key, &$array){

        $matchedArrayReffarance = null;

        if( !isset($array[$key]) ){

            foreach ($array as &$sub){

                if(is_array($sub)){
                    $matchedArrayReffarance = &searchKeyInArray($key, $sub);
                }
            }
        }else{
            $matchedArrayReffarance = &$array;
        }

        return $matchedArrayReffarance;
}

Then simply using above defined searchKeyInArray you can achieve what you are looking for

$returnHierarchy = array();
// This is example, in your case it is: $constants = $reflector->getConstants();
$constants = array(
        'Administrator' => "",
        'Account_Manager' => "Administrator",
        'BlogAuthor' => "Account_Manager",
        'CommentManager' => "Account_Manager",
);

foreach ($constants as $key => $value) {

    $matchArray = &searchKeyInArray($value, $returnHierarchy);

    if( isset($matchArray) ){
        $matchArray[$value][$key] = array();
    }else{
        $returnHierarchy[$key] = array();
    }
}

var_dump($returnHierarchy);

The var_dump of $returnHierarchy will be

array(1) { 
    ["Administrator"]=> &array(1) { 
          ["Account_Manager"]=> array(2) { 
              ["BlogAuthor"]=> array(0) { } 
              ["CommentManager"]=> array(0) { } 
          } 
    } 
}
Armen
  • 4,064
  • 2
  • 23
  • 40
  • Hello Armen, thanks for your answer. But could you please add the output which you get when running? When I run it, then I get nearly the same output as shown in question. Will be much appreciated :) – Snickbrack Feb 17 '16 at 09:13