Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php函数数组返回?_Php_Arrays_Function - Fatal编程技术网

php函数数组返回?

php函数数组返回?,php,arrays,function,Php,Arrays,Function,我想知道是否有一种方法可以从内联返回数组的函数中返回数组值。如果我有一个函数,比如: class MyObj { public function myFunction() { return array('arrayIndex'=>'myValue'); } } 我希望能够做到这一点: $object = new MyObj(); $myValue = $object->myFunction()['arrayIndex']; //but th

我想知道是否有一种方法可以从内联返回数组的函数中返回数组值。如果我有一个函数,比如:

class MyObj
{
    public function myFunction()
    {
         return array('arrayIndex'=>'myValue');
    }
}
我希望能够做到这一点:

$object = new MyObj();
$myValue = $object->myFunction()['arrayIndex']; //but this doesn't work
与此相反:

 $object = new MyObj();
 $myArray = $object->myFunction();
 $myValue = $myArray['arrayIndex'];

这个问题很简单,但我不知道是否有可能以类似的方式引用它。是的,还是不是?

升级到PHP5.4,然后就可以了。

升级到PHP5.4,然后就可以了。

怎么样

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($index)
    {
        $your_array = array('arrayIndex'=>'myValue');
        return $your_array[$index];
    }
}


f$object = new MyObj();
$myValue = $object->myFunction('arrayUndex');

发帖前请做好作业。您可以在这里的手册中找到:请在发布问题之前完成您的家庭作业。您可以在以下手册中找到: