Code: Select all
$pathinfo = pathinfo( "/home/somedir" );
$somevar = $pathinfo['dirname'];
Code: Select all
$somevar = pathinfo( "/home/somedir" )['dirname'];
Moderator: General Moderators
Code: Select all
$pathinfo = pathinfo( "/home/somedir" );
$somevar = $pathinfo['dirname'];
Code: Select all
$somevar = pathinfo( "/home/somedir" )['dirname'];
Code: Select all
$somevar = pathinfo("/home/somedir",PATHINFO_DIRNAME);Code: Select all
$somevar = dirname("/home/somedir");Code: Select all
function somefunc()
{
return array('one'=>1, 'two'=>2, 'three'=>3);
}
// this doesn't work.
$somevar = somefunc()['two'];
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.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?
Code: Select all
function somefunc($entry)
{
return array_intersect_key(array('one'=>1, 'two'=>2, 'three'=>3),array($entry=>NULL));
}
$somevar = somefunc('two');
Code: Select all
function somefunc()
{
return array('one'=>1, 'two'=>2, 'three'=>3);
}
$somevar = array_intersect_key(somefunc(),array('two'=>NULL));
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.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.
Code: Select all
function foo() {
return range(1,5);
}
list(, , $three) = foo();
echo $three;
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;
Simple answer: can't be done in PHP.jpodtbc wrote:it has to do with accessing an array element from a function return without first assigning to a variable