【问题标题】:Remove Switch-statement删除 Switch 语句
【发布时间】:2014-03-04 20:07:02
【问题描述】:

我有以下功能:

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 版本:http://pastebin.com/B9vU38nY

我想知道是否可以删除此函数的 switch 语句,同时仍然具有相同的结果。 $splitData 的大小有时会超过 20,而 20 大小写的 switch 语句看起来既丑陋又错误。我对 PHP 有很好的了解,但到目前为止我还想不出一种方法来完善这个函数。

【问题讨论】:

  • 你不能在 $splitData 上做一个 foreach() 循环吗?

标签: php algorithm function switch-statement case


【解决方案1】:

您可以使用引用创建这样的层次结构。

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;
    }
}

示例:http://codepad.viper-7.com/laXTVS

【讨论】:

    【解决方案2】:

    将此作为 for 循环执行。反转您的数组 $splitData 以便您可以从基本级别构建它并级联。这样,在循环的每次迭代中,您都可以将层次结构中进一步向下的元素级联到当前级别,直到到达顶部。

    代码留给读者作为练习

    【讨论】:

      【解决方案3】:

      这看起来像一个三,你可以使用递归... 但是您可以定义一个节点 oop 在这种情况下会有所帮助:

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

      每个节点包含一个子数组

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

      如果你想使用层次结构,你可以使用它

      【讨论】:

        【解决方案4】:

        我想 $this->hierarchy 在每次调用 generateStructureArray 之前都是空数组。你可以简单地用 for 循环构造数组:

        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;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多