2012-08-25 68 views
0

我一直在試圖解決這個問題,但找不到我能理解的解決方案。遞歸地重命名數組鍵

我有這個數組,當我的應用程序通過讀取動態配置文件加載時,這是在飛行中創建的。

我的問題是,我想要一個名爲alias的數組,它將被拾取並重命名['@attr']所在的父數組。這裏是一個例子來展示我的意思。

[database] => Array 
     (
      [0] => Array // Would be renamed to "production" 
       (
        [host] => test 
        [user] => test 
        [pass] => test 
        [name] => test 
        [port] => test 
        [@attr] => Array 
         (
          [alias] => production 
          [name] => live 
         ) 

       ) 

      [1] => Array // Would be renamed to "development" 
       (
        [user_data] => Array 
         (
          [0] => Array // Would be renamed to "three" 
           (
            [user] => sample 
            [@attr] => Array 
             (
              [alias] => three 
             ) 

           ) 

          [1] => Array // Would be renamed to "two" 
           (
            [user] => sample 
            [@attr] => Array 
             (
              [alias] => two 
             ) 

           ) 

         ) 

        [host] => test 
        [user] => test 
        [pass] => test 
        [name] => test 
        [port] => test 
        [@attr] => Array 
         (
          [alias] => development 
         ) 

       ) 

     ) 

這是到目前爲止我的代碼

private function _applyXmlAlias($array) 
     { 
      foreach ($array as $config) 
      { 
       if (is_array($config)) 
       { 
        if (isset($config['@attr']['alias'])) 
        { 
         $alias = $config['@attr']['alias']; 

         unset($config['@attr']['alias']); 

         if (empty($config['@attr'])) 
         { 
          unset($config['@attr']); 
         } 

         $this->_alias[$alias] = $config; 

         break; 

         //$this->_temp = reset(); 


        } else { 

         $this->_applyXmlAlias($config); 
        } 
       } 
      } 
     } 

如果我做我的_alias財產的print_r的我得到這個

Array 
(
    [production] => Array 
     (
      [host] => test 
      [user] => test 
      [pass] => test 
      [name] => test 
      [port] => 3306 
      [@attr] => Array 
       (
        [name] => live 
       ) 

     ) 

    [development] => Array 
     (
      [user_data] => Array 
       (
        [0] => Array 
         (
          [user] => sample 
          [@attr] => Array 
           (
            [alias] => three 
           ) 

         ) 

        [1] => Array 
         (
          [user] => sample 
          [@attr] => Array 
           (
            [alias] => two 
           ) 

         ) 

       ) 

      [host] => test 
      [user] => test 
      [pass] => test 
      [name] => test 
      [port] => 3306 
     ) 

) 

注:我不知道爲什麼只有我的循環使用密鑰alias捕獲最後一個嵌套數組,而不是其他具有值productiondevelopment

關於如何處理此問題的任何想法?我覺得我很接近,只需要一個正確的道路上的小指南。

感謝

編輯:更新我的功能,以反映當前attemps

+0

需要更多的休息...看到我下面的評論。 – EthanB

回答

0

你的算法是做了深度優先搜索。問題是它不知道何時終止。

相反void返回型,嘗試這樣的事情:

private function _applyXmlAlias($array) 
    { 
     $found_alias = FALSE; 
     foreach ($array as $config) 
     { 
      if (is_array($config)) 
      { 
       if (isset($config['@attr']['alias'])) 
       { 
        $this->_alias = reset(array_values($config)); 
        $found_alias = TRUE; 
         break; 
       } else { 
        if ($this->_applyXmlAlias($config)) { 
         $found_alias = TRUE; 
         break; 
        } 
       } 
      } 
     } 
     return $found_alias; 
    } 
+0

你會推薦什麼?我應該在那裏扔一個'break'嗎?如果我嘗試了類似的方法,但最終只能從其中一個鍵獲取值。我已更新我的代碼以顯示我的最新嘗試 – Eli

+0

更新了示例...中斷不會完全解決它,您需要一種方法來告訴調用者(它是深度優先),您已找到某些內容並應該中斷也是。 – EthanB

0

很有可能不是最完美的解決方案,但工程。

function renameArr($arr) { 
    foreach($arr as $key=>$val) { 
     if(is_array($val)) { 
      if(isset($val['@attr']['alias'])) { 
       unset($arr[$key]); 
       $key = $val['@attr']['alias']; 
      } 
      $arr[$key] = renameArr($val); 
     } 
    } 
    return $arr; 
}