2016-11-21 18 views
-1

Symfony的嵌套YAML和PHP數組轉換文件轉換爲點符號,就像這樣:modules.module.title轉換多維數組爲點記號在Symfony2中

我正在寫一些代碼,出口YAML翻譯文件的數據庫,我需要解析的文件拼合到一個點符號。

有誰知道它的功能的Symfony採用扁平化嵌套數組來點記號?

我不能在源代碼中的任何地方找到它。

回答

1

這是flatten()方法Symfony\Component\Translation\Loader\ArrayLoader

<?php 

/** 
* Flattens an nested array of translations. 
* 
* The scheme used is: 
* 'key' => array('key2' => array('key3' => 'value')) 
* Becomes: 
* 'key.key2.key3' => 'value' 
* 
* This function takes an array by reference and will modify it 
* 
* @param array &$messages The array that will be flattened 
* @param array $subnode Current subnode being parsed, used internally for recursive calls 
* @param string $path  Current path being parsed, used internally for recursive calls 
*/ 
private function flatten(array &$messages, array $subnode = null, $path = null) 
{ 
    if (null === $subnode) { 
     $subnode = &$messages; 
    } 
    foreach ($subnode as $key => $value) { 
     if (is_array($value)) { 
      $nodePath = $path ? $path.'.'.$key : $key; 
      $this->flatten($messages, $value, $nodePath); 
      if (null === $path) { 
       unset($messages[$key]); 
      } 
     } elseif (null !== $path) { 
      $messages[$path.'.'.$key] = $value; 
     } 
    } 
}