简体   繁体   English

在PHP中对多维数组进行分组

[英]Grouping multi-dimensional arrays in PHP

I can't focus right now and my mind keeps playing tricks on me with the solution to this. 我现在无法集中精力,我的脑子一直在想办法解决这个问题。

I've tried a multitude of options but they keep not working.. Sigh. 我尝试了多种选择,但它们仍然无法正常工作。

Say I have a string as such; 说我有这样的字符串; abc|abd|abe|f|fg|fh|i , I want to create a new array (or object) as the following abc|abd|abe|f|fg|fh|i ,我想创建一个新的数组(或对象),如下所示

A > B > C, D, E    
F > G, H    
I

With > being a nested array, , being an element in the parent array. 随着>是一个嵌套数组, ,作为父阵列中的元素。

These should be able to continue nesting a multitude of times, eg A > B > C > D > E > F, D 这些应该能够继续嵌套​​多个时间,例如A > B > C > D > E > F, D

Any guidance? 有指导吗? I've tried exploding the string and then those strings to an array - this array holds A > B > C, A > B > D, A > B > E etc., I just can't get my head around to how to combine them efficiently. 我试过将字符串A > B > C, A > B > D, A > B > E然后将这些字符串A > B > C, A > B > D, A > B > E为数组-该数组包含A > B > C, A > B > D, A > B > E等,我只是无法理解如何有效地结合它们。

I started with just looping over each element in the array and checking if the parents' key exists, but that was failing. 我首先循环遍历数组中的每个元素,并检查父键是否存在,但这失败了。 Any help is appreciated as I'm incredibly tired, quite shocking I can do such a simple task. 感谢我的帮助,因为我非常累,我可以做这样一个简单的任务,实在令人震惊。

<?php

// read line from standard input
$line = trim(fgets(STDIN));

echo "Line: $line\n"; // debug

// split lines by |
$segments = explode('|', $line);

print_r($segments); // debug

// prepare output array
$md_array = array();

// walk through the segments
foreach($segments as $segment) {
    // set pointer to output array
    $current = &$md_array;
    // split segment by .
    $tokens = explode('.', $segment);

    print_r($tokens); // debug

    foreach($tokens as $token) {
        echo "working on $token\n";

        // if key is not included in the array, create empty array
        if( ! array_key_exists($token, $current) ) {
            $current[$token] = array();
        }
        // pass the pointer to following sub-section
        $current = &$current[$token];
    }
}

// print out the output
print_r($md_array);

?>

Testing the script 测试脚本

echo "a.b.c|a.b.d|a.b.e|f|f.g|f.h|i" | php test.php

Output 产量

Array
(
    [a] => Array
        (
            [b] => Array
                (
                    [c] => Array
                        (
                        )

                    [d] => Array
                        (
                        )

                    [e] => Array
                        (
                        )

                )

        )

    [f] => Array
        (
            [g] => Array
                (
                )

            [h] => Array
                (
                )

        )

    [i] => Array
        (
        )

)

Want to thank Richard for his solution, which allowed me to achieve my aim. 感谢Richard的解决方案,它使我得以实现自己的目标。

See the code below: 请参见下面的代码:

public static function permissions($permList) {
    $segments = explode('|', $permList);
    $md_array = array();
    foreach($segments as $segment) {
        $current = &$md_array;
        $tokens = explode('.', $segment);
        $x = 0;
        foreach($tokens as $token) {                
            if(!array_key_exists($token, $current) ) {
                if(count($tokens) > $x+1)
                    $current[$token] = array();
                else
                    $current[] = $token;
            }
            if(count($tokens) > $x+1)
                $current = &$current[$token];
            $x++;
        }
    }
    return $md_array;
}   

If I input abc|abd|abe|f|fg|fh|i , I am returned with the expected output above. 如果我输入abc|abd|abe|f|fg|fh|i ,则返回上面的预期输出。

Many thanks. 非常感谢。

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

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