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