Php convert string into array for group?

403 Views Asked by At

I have group on string i want to explode by group and sub group

For example

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

In want result as following:

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

Can anybody give solution how i can convert string to array as above array?

3

There are 3 best solutions below

3
On BEST ANSWER

Assuming that is one string value, this is probably as efficient as you can get:

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

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
                )    
        )    
)
0
On

If you store your strings into an array then you can do this like below

<?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>';

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

Thanks Nick for your answer.