Skip to content

Instantly share code, notes, and snippets.

@blar
Created February 14, 2014 14:50
Show Gist options
  • Select an option

  • Save blar/9002307 to your computer and use it in GitHub Desktop.

Select an option

Save blar/9002307 to your computer and use it in GitHub Desktop.
array_path
<?php
function array_path($array, $path) {
$context = $array;
$parts = explode('.', $path);
while($key = array_shift($parts)) {
if(!is_array($context)) {
return NULL;
}
if(!array_key_exists($key, $context)) {
return NULL;
}
$context = $context[$key];
}
return $context;
}
$array['a']['b']['c']['d']['e'] = 42;
var_dump(array_path($array, 'a.b.c.d.e'));
var_dump(array_path($array, 'a.b.c.d.e.f'));
var_dump(array_path($array, 'a.b.c.d'));
int(42)
NULL
array(1) {
["e"]=>
int(42)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment