1

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?

1

3 Answers 3

5

Upgrade to PHP 5.4 and you can then do array dereferencing.

Sign up to request clarification or add additional context in comments.

Comments

0

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');

1 Comment

what the f ??
0
class MyObj
{
    public function myFunction()
    {
         return array($one,$two);
    }
}


f$object = new MyObj();
list($first,$second) = $object->myFunction();

1 Comment

What the f ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.