lookup($foo, array('bar', 'baz' 15, 'quux'))
This is equivalent to any of the following: $foo->bar->baz[15]->quux
$foo->bar->baz[15]['quux']
$foo->bar['baz'][15]->quux
$foo->bar['baz'][15]['quux']
$foo['bar']->baz[15->quux
... and so on
It's useful in Drupal when the required data is often at the end of a long chain of objects-containing-arrays-containing-objects-....I've been a bit naughty with its types for the sake of convenience: if a non-array is given as the second argument, it's wrapped as a singleton array, ie.
lookup(array('foo' => 'bar'), 'foo') === 'bar'
Also if any of the components aren't found, it returns NULL, ie. lookup(array('foo' => NULL), 'foo') === lookup(array(), 'foo')
It would be theoretically better to return an empty array on error and a singleton array on success, since they can be distinguished, but in practice that's so far down the list of problems with PHP that it's not worth the effort of adding such wrappers at the moment.A really nice thing about this function is that it curries nicely:
$find_in_foo = partially_apply('lookup', $foo)
$get_blah = partially_apply(flip('lookup'), 'blah')
$find_in_foo('x') === lookup($foo, 'x')
$get_blah($foo) === lookup($foo, 'blah')
Actually, my argument-flipping function is already curried, so I can just say: $get_blah = flip('lookup', $foo)
This currying is great for filtering, mapping, etc.