CodeIgniter Forums
[Basic PHP] Treat function or method call as an array? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: [Basic PHP] Treat function or method call as an array? (/thread-26207.html)



[Basic PHP] Treat function or method call as an array? - El Forum - 01-08-2010

[eluser]janogarcia[/eluser]
I have these values

Code:
$CI->config->item('returns_indexed_array');
$CI->config->item('returns_index');

This works...

Code:
$array = $CI->config->item('returns_indexed_array');
$index = $CI->config->item('returns_index');

$value = $array[$index];

...but, what if I don't want to create those temporary variables $array and $index? Is it possible to get the value in one shot?

This wouldn't work... Any simple workaround? Am I missing something?
Code:
$value = $CI->config->item('returns_indexed_array')[$CI->config->item('returns_index')];

Found in stack overflow:
Quote:Unfortunately, you cannot treat function return values as arrays in PHP. You have to assign the value explicitly.



[Basic PHP] Treat function or method call as an array? - El Forum - 01-08-2010

[eluser]danmontgomery[/eluser]
Like the stack overflow post says, you can't treat return values as arrays. You can get rid of one of your lines, though.

Code:
$array = $CI->config->item('returns_indexed_array');
$value = $array[$CI->config->item('returns_index')];

You could also just write a helper function so that it would at least look cleaner.


[Basic PHP] Treat function or method call as an array? - El Forum - 01-08-2010

[eluser]janogarcia[/eluser]
Thank you noctrum for the fast reply.

(the snippet I posted is not actual production code, it was an illustrative example, I could have used the config_item() helper function of Common.php to the same effect: $array = config_item('some_array'); $index = config_item('some_index');...)


[Basic PHP] Treat function or method call as an array? - El Forum - 01-08-2010

[eluser]danmontgomery[/eluser]
You can use array_* functions on the result, which may not be much help if you're looking for a specific index, bout you could do something like

Code:
$value = array_pop($CI->config->item('returns_indexed_array'));

Or any (I haven't tested them all, but I'm thinking any of them should work) of the array functions... Depending on your specific needs you may be able to work something out that way.


[Basic PHP] Treat function or method call as an array? - El Forum - 01-09-2010

[eluser]janogarcia[/eluser]
Just found this snippet at the PHP documentation (curiosly enough it was submitted a few weeks ago)

http://www.php.net/manual/en/language.types.array.php#95210

Quote:If you ever wondered if you can do something like:

Code:
<?php
$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".

Code:
<?php

/**
* Usage: i( $array, $index [, $index2, $index3 ...] )
*
* This is functionally equivalent to $array[$index1][$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
* described above, for improving legibility.
*
* @param $array
* @param $index
* @param [optional] $index2, index3, ...
* @throws Exception when the indexes do not exist
*/
function i(){
    $args = func_get_args();
    $array = $args[0];//gets the fist parameter, $array
    $indexes = $args;
    unset($indexes[0]);//because indexes[0] is actually not an index, but the first parameter, $array
    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;      
}
?>

This could be an interesting addition to the array_helper.php (I'd modify the error handling routine to be more "CI friendly") if you find yourself using many unnecessary temporary variables.