danielharan (owner)

Revisions

gist: 138521 Download_button fork
public
Public Clone URL: git://gist.github.com/138521.git
Embed All Files: show embed
PHP #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# http://svn.facebook.com/svnroot/platform/samples/packages/
# therunaround.tar.gz / core.php
/**
* Returns $arr[$idx], because php doesn't let you index into
* arrays returned from a function.
*
* a()[0] doesn't work
*
* idx(a(), 0) does.
*
* PHP is a pretty stupid language.
*
* @param array to index into
* @param index. if negative, start at the end.
* @param default to return if $arr[$idx] is not set
* @return array[index]
*/
function idx($arr, $idx, $default=null) {
if ($idx === null || !is_array($arr)) {
return $default;
}
$idx = $idx >= 0 ? $idx : count($arr) + $idx;
return array_key_exists($idx, $arr) ? $arr[$idx] : $default;
}