Skip to content

Instantly share code, notes, and snippets.

@sveneisenschmidt
Created March 4, 2011 13:21
Show Gist options
  • Save sveneisenschmidt/854601 to your computer and use it in GitHub Desktop.
Save sveneisenschmidt/854601 to your computer and use it in GitHub Desktop.
function array_path($array, $path) {
$matches = preg_split("/\[|\]/", $path, -1, PREG_SPLIT_NO_EMPTY);
if(empty($matches)) {
return null;
}
$data = $array;
foreach($matches as $key) {
if(!array_key_exists($data, $matches)) {
return null;
}
$data = $data[$key];
}
return $data;
}
$data = array(
'one' => array(
'two' => 'three'
),
'four' => array(
'five' => 'six'
)
);
$result = array_path($data, 'one[two]');
print_r($result);
// outputs: three
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment