php - String to multidimensional array path -
i have multidimensional array, here little excerpt:
array ( [albums] => array ( [a great big world - there out there] => array(...), [atb - contact] => array(...), ) [pop] => array (...) ) and have dynamic path:
/albums/a_great_big_world_-_is_there_anybody_out_there what best way retrieve value of (in example) $arr["albums"]["a great big world - there out there"]?
please note should dynamic, since nesting can go deeper 2 levels in example.
edit
here function utilize create simple string url:
function formaturl($url) { homecoming preg_replace('/__+/', '_', preg_replace('/[^a-z0-9_\s-]/', "", strtolower(str_replace(" ", "_", $url)))); } $array = array(...); $path = '/albums/a_great_big_world_-_is_there_anybody_out_there'; $value = $array; foreach (explode('/', trim($path, '/')) $key) { if (isset($value[$key]) && is_array($value[$key])) { $value = $value[$key]; } else { throw new exception("path $path invalid"); } } echo $value;
php arrays multidimensional-array
No comments:
Post a Comment