-1

我有字符串组我想按组和子组爆炸

例如

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

任何人都可以提供解决方案我如何将字符串转换为上述数组?

4

3 回答 3

1

假设这是一个字符串值,这可能是你能得到的最有效的:

$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
                )    
        )    
)
于 2018-10-02T06:29:04.023 回答
0

如果您将字符串存储到数组中,那么您可以像下面这样执行此操作

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

        )

)
于 2018-10-02T07:54:00.480 回答
0
$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);

感谢尼克的回答。

于 2018-10-03T06:26:44.110 回答