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

时间:2018-10-02 06:12:22

标签: php multidimensional-array explode string-function

我有一个要按组和子组爆炸的字符串组

例如

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

有人可以给出解决方案,如何将字符串转换为上述数组吗?

3 个答案:

答案 0 :(得分: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
                )    
        )    
)

答案 1 :(得分: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
                )

        )

)

答案 2 :(得分: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);

感谢尼克的回答。