I was wondering if there is a way to return an array value from a function that returns an array inline. So if I have a function like:
class MyObj
{
public function myFunction()
{
return array('arrayIndex'=>'myValue');
}
}
I would like to be able to do this:
$object = new MyObj();
$myValue = $object->myFunction()['arrayIndex']; //but this doesn't work
rather than this:
$object = new MyObj();
$myArray = $object->myFunction();
$myValue = $myArray['arrayIndex'];
Simple question but I just don't know if its possible to reference it in a similar way. So yay or nay?
Upgrade to PHP 5.4 and you can then do array dereferencing.
What about
class MyObj
{
public function myFunction($index)
{
$your_array = array('arrayIndex'=>'myValue');
return $your_array[$index];
}
}
f$object = new MyObj();
$myValue = $object->myFunction('arrayUndex');
class MyObj
{
public function myFunction()
{
return array($one,$two);
}
}
f$object = new MyObj();
list($first,$second) = $object->myFunction();