array access short-hand

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jpodtbc
Forum Newbie
Posts: 4
Joined: Wed Apr 29, 2009 3:44 pm

array access short-hand

Post 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.
Last edited by Benjamin on Wed Apr 29, 2009 4:16 pm, edited 1 time in total.
Reason: Added code tags.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: array access short-hand

Post by Mark Baker »

Code: Select all

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

Code: Select all

$somevar = dirname("/home/somedir");
jpodtbc
Forum Newbie
Posts: 4
Joined: Wed Apr 29, 2009 3:44 pm

Re: array access short-hand

Post 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?
Last edited by Benjamin on Wed Apr 29, 2009 4:17 pm, edited 1 time in total.
Reason: Added code tags.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: array access short-hand

Post 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)); 
 
jpodtbc
Forum Newbie
Posts: 4
Joined: Wed Apr 29, 2009 3:44 pm

Re: array access short-hand

Post 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.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: array access short-hand

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: array access short-hand

Post 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;
 
 
jpodtbc
Forum Newbie
Posts: 4
Joined: Wed Apr 29, 2009 3:44 pm

Re: array access short-hand

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: array access short-hand

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply