简体   繁体   English

使用更简单的PHP数组中的键创建多维数组

[英]Creating a multi-dimensional array with keys from a simpler PHP array

I have an array of documents, where each document have another simple one-dimensional array of facets (simple textual labels attached to the document) that have structural value in their order (0 from nearest the root to the edges). 我有一个文档数组,其中每个文档都有另一个简单的一维facet数组(附加到文档的简单文本标签),它们的顺序具有结构值(0从最近的根到边缘)。 I'm traversing this array, and would like to create a multi-dimensional array, like a tree-structure. 我正在遍历这个数组,并且想要创建一个多维数组,就像树结构一样。 So, having something like this fragment of just one of those documents ; 因此,只有一部分这样的文档具有这种片段;

Array ( 'document-001' => Array (
   Array ( 'facets' => array (
      'Public - Policy and Procedures',
      'Employment Services Manual',
      'Section 02 - Recruitment & Selection',
   )
   ... many more here ...
) ;

I want this ; 我要这个 ;

Array
(
    [Public - Policy and Procedures] => Array (
            [Administration Manual] => Array ( )
            [Corporate Governance Manual] => Array ( )
            [Food Services Manual] => Array ( )
            [Charter Manual] => Array ( )
            [Infection Control Manual] => Array ( )
            [Leisure and Lifestyle Manual] => Array ( )
            [Employment Services Manual] => Array (
                    [Section 09 - Termination & Resignation] => Array ( )
                    [Section 02 - Recruitment & Selection] => Array ( )
                    [Section 10 - Security] => Array ( )
            )
            [Environmental Sustainability Manual] => Array (
                    [Property - New Development & Refurbishment Policy 5.5] => Array ( )
            )
     )

My current solution is highly inelegant, where $index is my new multi-dimensional array ; 我当前的解决方案非常优雅,其中$ index是我新的多维数组;

// Pick out the facets array from my larger $docs array
$t = $docs['facets'] ;
$c = count ( $t ) ;

if      ( $c == 2 ) $index[$t[0]] = array() ;
else if ( $c == 3 ) $index[$t[0]][$t[1]] = array() ;
else if ( $c == 4 ) $index[$t[0]][$t[1]][$t[2]] = array() ;
else if ( $c == 5 ) $index[$t[0]][$t[1]][$t[2]][$t[3]] = array() ;
else if ( $c == 6 ) $index[$t[0]][$t[1]][$t[2]][$t[3]][$t[4]] = array() ;
else if ( $c == 7 ) $index[$t[0]][$t[1]][$t[2]][$t[3]][$t[4]][$t[5]] = array() ;

Surely there's a better way. 当然有更好的方法。 I've troddled through the various array functions, but nothing stands out as an obvious solution. 我已经厌倦了各种数组函数,但没有什么是显而易见的解决方案。 The problem here is that the dynamicism battles against the syntax of PHP itself. 这里的问题是动态主义与PHP本身的语法作斗争。 I could of course create an OO solution, but this is such a simple little traverse I don't really want to go there (even though I probably should). 我当然可以创建一个OO解决方案,但这是一个如此简单的小遍历,我真的不想去那里(即使我可能应该)。

Thoughts? 思考?

Just use some recursion: 只需使用一些递归:

function bar($source, $dest){
    if(count($source) == 0){
        return array();
    }
    $dest[$source[0]] = bar(array_slice($source, 1), $dest[$source[0]]);
    return $dest;
}

$t = $docsA['facets'];
$s = $docsB['facets'];

$index = array();
$index = bar($t, $index);
$index = bar($s, $index);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM