Question

I have the following function:

private function generateStructureArray($file) {
    $splitData = explode('/', $file);
    switch(count($splitData)) {
    case 1:
        $this->hierarchy[] = $splitData[0];
        break;
    case 2:
        $this->hierarchy[$splitData[0]][] = $splitData[1];
        break;
    case 3:
        $this->hierarchy[$splitData[0]][$splitData[1]][] = $splitData[2];
        break;
    case 4:
        $this->hierarchy[$splitData[0]][$splitData[1]][$splitData[2]][] = $splitData[3];
        break;
    case 5:
        $this->hierarchy[$splitData[0]][$splitData[1]][$splitData[2]][$splitData[3]][] = $splitData[4];
        break;
}

Pastebin-version: http://pastebin.com/B9vU38nY

I'm wondering if it is possible to remove the switch statement for this function while still having the same result. The size of $splitData sometimes can be over 20, and a 20-case switch statement seems ugly and wrong. I have a pretty good knowledge of PHP, but so far I was unable to think of a way to pretty-fy this function.

Was it helpful?

Solution

You can create a hierarchy like this using references.

private function generateStructureArray($file) {
    //split the file into paths
    $splitData = explode('/', $file);
    //pop off the filename
    $fileName = array_pop($splitData);

    //create a temp reference to the hierarchy. Need a temp var
    //because this will get overwritten again and again.
    $tmp = &$this->hierarchy;

    //loop over the folders in splitData
    foreach($splitData as $folder){
        //check if the folder doesn't already exists
        if(!isset($tmp[$folder])){
            //folder doesn't exist so set the folder to a new array
            $tmp[$folder] = array();
        }
        //re-set tmp to a reference of the folder so we can assign children
        $tmp = &$tmp[$folder];
    }

    //now we have the folder structure, but no file
    //if file is not empty, add it to the last folder
    if(!empty($fileName)){
        $tmp[] = $fileName;
    }
}

Example: http://codepad.viper-7.com/laXTVS

OTHER TIPS

Do this as a for loop. Reverse your array $splitData so that you can build it from the base level and cascade up. This way, with each iteration of the loop, you can cascade elements from further down in your hierarchy into the current level, until you've reached the top.

code left as an exercise to the reader

this looks like a three, you can use recursion... however you can define a node oop will help in this case:

class Node {
   var $Childrens; //array of childrens
}

each Node contains an array of children

class Three {
 var $root = new Node();
}

you can use this if you want to use a hierarchycal structure

i suppose $this->hierarchy is empty array before each call of generateStructureArray. You can simply construct array with for loop:

private function generateStructureArray($file) {
    $splitData = array_reverse(explode('/', $file));
    $result = array(array_pop($splitData));
    foreach($splitData as $element) {
      $result = array($element => $result);
    }
    $this->hierarchy = $result;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top