Check parse_str()
:
parse_str('set[0][p1]', $AR);
Oh, you want to access the index of the array... Here is my take:
getValue($AR, array('set', 0, 'p1'));
Or if you really must use the original string representation:
parse_str('set[0][p1]', $keys);
getValue($AR, $keys);
Disclaimer: I haven't tested this, you might need to use array_keys()
somewhere.
And the helper function:
function getValue($array, $key, $default = false)
{
if (is_array($array) === true)
{
settype($key, 'array');
foreach ($key as $value)
{
if (array_key_exists($value, $array) === false)
{
return $default;
}
$array = $array[$value];
}
return $array;
}
return $default;
}
I would avoid regexing your way into this problem.