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

发布于 2024-10-02 16:30:48 字数 1957 浏览 0 评论 0原文

我有一个文档数组,其中每个文档都有另一个简单的一维构面数组(附加到文档的简单文本标签),这些构面按其顺序具有结构值(从最近的根到边缘的 0)。我正在遍历这个数组,并且想创建一个多维数组,例如树结构。因此,只有其中一个文档中有这样的片段;

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

我想要这个;

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 ( )
            )
     )

我当前的解决方案非常不优雅,其中 $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() ;

当然有更好的方法。我已经研究过各种数组函数,但没有什么是明显的解决方案。这里的问题是动态性与 PHP 本身的语法相冲突。我当然可以创建一个面向对象的解决方案,但这是一个如此简单的小遍历,我真的不想去那里(尽管我可能应该这样做)。

想法?

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). 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 ;

// 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. 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).

Thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

末蓝 2024-10-09 16:30:48

只需使用一些递归:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文