Php 将字符串转换为组数组?

Php convert string into array for group?

我在字符串上有组我想按组和子组展开

例如

operation.user.add
operation.user.edit
operation.permission.add
performance.tool.add
performance.tool.remove
operation.permission.edit
operation.tool.delete
operation.tool.remove

想要的结果如下:

Array
(
['operation'] => Array
    (
        ['user'] => Array
            (
                [0] => 'add',
                [1] => 'edit'
            ),
        ['permission'] => Array
            (
                [0] => 'add',
                [1] => 'edit'
            ),
        ['tool'] => Array
            (
                [0] => 'delete',
                [1] => 'remove'
            ),
    ),
['performance'] => Array
    (
        ['tool'] => Array
            (
                [0] => 'add',
                [1] => 'remove'
            )
    )
)

任何人都可以解决我如何将字符串转换为上述数组的数组吗?

假设这是一个字符串值,这可能是您可以获得的最有效的方法:

$str = 'operation.user.add
operation.user.edit
operation.permission.add
performance.tool.add
performance.tool.remove
operation.permission.edit
operation.tool.delete
operation.tool.remove';
$output = [];
foreach (explode("\n", $str) as $string) {
    $parts = explode('.', $string);
    $output[$parts[0]][$parts[1]][] = $parts[2];
}
print_r($output);

输出:

Array
(
    [operation] => Array
        (
            [user] => Array
                (
                    [0] => add
                    [1] => edit
                )    
            [permission] => Array
                (
                    [0] => add
                    [1] => edit
                )    
            [tool] => Array
                (
                    [0] => delete
                    [1] => remove
                )    
        )    
    [performance] => Array
        (
            [tool] => Array
                (
                    [0] => add
                    [1] => remove
                )    
        )    
)

如果您将字符串存储到一个数组中,那么您可以像下面那样做

<?php 

$string = [
    "operation.user.add",
    "operation.user.edit",
    "operation.permission.add",
    "performance.tool.add",
    "performance.tool.remove",
    "operation.permission.edit",
    "operation.tool.delete",
    "operation.tool.remove",
];

$result = [];
for( $i = 0; $i < count( $string ); $i++ ) {
    $parts = explode( '.', $string[$i] );
    $result[$parts[0]][$parts[1]][] = $parts[2];

}
echo '<pre>';
print_r($result);
echo '</pre>';

输出:

Array
(
    [operation] => Array
        (
            [user] => Array
                (
                    [0] => add
                    [1] => edit
                )

            [permission] => Array
                (
                    [0] => add
                    [1] => edit
                )

            [tool] => Array
                (
                    [0] => delete
                    [1] => remove
                )

        )

    [performance] => Array
        (
            [tool] => Array
                (
                    [0] => add
                    [1] => remove
                )

        )

)
$str = 'operation.user.add
operation.user.edit
operation.permission.add
performance.tool.add
performance.tool.remove
operation.permission.edit
operation.tool.delete
operation.tool.remove';
$output = [];
foreach (explode("\n", $str) as $string) {
    $parts = explode('.', $string);
    $output[$parts[0]][$parts[1]][] = $parts[2];
}
print_r($output);

感谢尼克的回答。