I want to generate a nested array from a string.
The String looks like this
$item = "profile:my_wallet:btn_recharge";
I want to convert it to nested array like this
["profile"]["my_wallet"]["btn_recharge"]
I want to generate a nested array from a string.
The String looks like this
$item = "profile:my_wallet:btn_recharge";
I want to convert it to nested array like this
["profile"]["my_wallet"]["btn_recharge"]
What you could use is a simple recursion to go as deep as you like.
function append(array $items, array $array = []): array
{
$key = array_shift($items);
if (!$key) return $array;
$array[$key] = append($items, $array);
return $array;
}
$array = append(explode(':', "profile:my_wallet:btn_recharge"));
The result of $array
looks like below and can be accessed as you asked
$array['profile']['my_wallet']['btn_recharge'];
array (
'profile' =>
array (
'my_wallet' =>
array (
'btn_recharge' =>
array (
),
),
),
)