Have an account? Sign in
Login  Register  Facebook
This Page is Under Construction! - If You Want To Help Please Send your CV - Advanced Web Core (BETA)
[Edit] An equivalent to $array[$index1][$index2]
If you ever wondered if you can do something like:
$a = function_that_returns_an_array()['some_index']['some_other_index'] ;
The answer is no, you can't. But you can use the following function. I named it i() because it's a short name and stands for "to index".

Usage:

i( $array, $index [, $index2, $index3 ...] )

It can replace the more prolix

$tmp = some_function_that_returns_an_array() ;
$value = $tmp['some_index']['some_other_index'] ;
by doing the job with a single line of code as in
$value = i( some_function_that_returns_an_array(), 'some_index', 'some_other_index' ) ;
Note that since this function is slower than direct indexing, it should only be used in cases like the one
function i(){
    $args = func_get_args();
    $array = $args[0];
    $indexes = $args;
    unset($indexes[0]);
    foreach( $indexes as $index ){
        if( (! is_array($array)) || (! array_key_exists( $index, $array )) ){
            throw new Exception("Array index out of bounds.Parameters:".print_r($args,true));           
        }
        $array = $array[$index];
    }
    return $array;       
}