Page 1 of 1

array access short-hand

Posted: Wed Apr 29, 2009 3:49 pm
by jpodtbc
i'm a fairly advanced PHP programmer, but there's one thing that i've always wanted to know how to do. after much searching, i'm not sure it's possible. currently i have this:

Code: Select all

 
$pathinfo = pathinfo( "/home/somedir" );
$somevar = $pathinfo['dirname'];
 
what i want to do is access the array element in the same line as the function call. the following syntax does not work:

Code: Select all

 
$somevar = pathinfo( "/home/somedir" )['dirname']; 
 
but i'd love to know if there's a way to do this sort of thing without having to assign a temp variable.

Re: array access short-hand

Posted: Wed Apr 29, 2009 4:07 pm
by Mark Baker

Code: Select all

$somevar = pathinfo("/home/somedir",PATHINFO_DIRNAME);
or

Code: Select all

$somevar = dirname("/home/somedir");

Re: array access short-hand

Posted: Wed Apr 29, 2009 4:15 pm
by jpodtbc
obviously i should have used a more generic example.

it has NOTHING to do with pathinfo. it has to do with accessing an array element from a function return without first assigning to a variable.

Code: Select all

 
function somefunc()
{
    return array('one'=>1, 'two'=>2, 'three'=>3);
}
 
// this doesn't work.
$somevar = somefunc()['two']; 
 
how can i access a given element without assigning the return to a variable first?

Re: array access short-hand

Posted: Wed Apr 29, 2009 4:52 pm
by Mark Baker
jpodtbc wrote:it has NOTHING to do with pathinfo. it has to do with accessing an array element from a function return without first assigning to a variable.

how can i access a given element without assigning the return to a variable first?
The syntax of PHP is pretty simple and well defined... it may have it's quirks, but if you want a different syntax then you should be looking to another language.

Alternatively, write your functions to support what you actually want them to do

Code: Select all

 
function somefunc($entry)
{
    return array_intersect_key(array('one'=>1, 'two'=>2, 'three'=>3),array($entry=>NULL));
}
 
$somevar = somefunc('two'); 
 
or use

Code: Select all

 
function somefunc()
{
    return array('one'=>1, 'two'=>2, 'three'=>3);
}
 
$somevar = array_intersect_key(somefunc(),array('two'=>NULL)); 
 

Re: array access short-hand

Posted: Wed Apr 29, 2009 5:27 pm
by jpodtbc
wow, mark baker. you couldn't be more frustrating. just admit that there's no way to do it - that php cannot do what i'm asking and be done with it. not every function that returns an array is a custom function (hence my original example).

php has a lot of handy tricks that can be employed to reduce to less lines of code. in this case there is clearly no trick. two lines of code are required where there COULD be just one.

Re: array access short-hand

Posted: Thu Apr 30, 2009 2:50 am
by Mark Baker
jpodtbc wrote:wow, mark baker. you couldn't be more frustrating. just admit that there's no way to do it - that php cannot do what i'm asking and be done with it. not every function that returns an array is a custom function (hence my original example).
php has a lot of handy tricks that can be employed to reduce to less lines of code. in this case there is clearly no trick. two lines of code are required where there COULD be just one.
I've shown you two ways to achieve the result that you're asking for: one by modifying your function to return a single entry, one by modifying the line that calls the function.
Just because it can't be done the way you want to do it, doesn't mean that the answers you've been given are wrong. The resulting value in $somevar is what you wanted it to be: there is no trick, no variant syntax, just a simple function call to do what you want instead. There is no need for two lines of code or an extra variable.

There is no way to change the syntax of PHP without it becoming another language.
If it really matters to you that PHP should support this particular form of syntax, then go to the PHP source code and rewrite it to allow your variant syntax, or get involved in the development of PHP6, perhaps if you present a strong enough case for the language needing that syntax they'll be obliging.

Re: array access short-hand

Posted: Thu Apr 30, 2009 2:58 am
by Benjamin
@jpodtbc, please be nice, there's no reason to be upset. And here's your solution:

Code: Select all

 
function foo() {
    return range(1,5);
}
 
list(, , $three) = foo();
 
echo $three;
 
And just for fun, here's another solution:

Code: Select all

 
function array_get_key($array, $key) {
    return (isset($array[$key])) ? $array[$key] : null;
}
 
$three = array_get_key(range(1,5), 2);
 
echo $three;
 
 

Re: array access short-hand

Posted: Thu Apr 30, 2009 12:20 pm
by jpodtbc
fair enough.

i suppose a custom array accessing function included in all my projects would be the simplest way to go.

it's not that i NEED such functionality or feel that it must be there. it's just that on occasion the easiest way to do it is with 2 lines instead of 1.

Re: array access short-hand

Posted: Thu Apr 30, 2009 12:46 pm
by pickle
jpodtbc wrote:it has to do with accessing an array element from a function return without first assigning to a variable
Simple answer: can't be done in PHP.