1

假设我有这个:

function data() {
    $out['a'] = "abc";
    $out['b'] = "def";
    $out['c'] = "ghi";
    return $out;
}

我可以通过将数据声明为变量来输出数据,然后使用数组索引来回显它:

$data = data();
echo $data['a'];
echo $data['b'];
echo $data['c'];

但是,我正在调用与其他函数内联的函数,并且我试图避免必须先声明一个变量。例如,我想做这样的事情:

echo data()[0]; //pulls first value in array without declaring it as a variable first. This needs to be variable i.e. data()[1] data()[2] etc.

或者更具体地说,我实际上是在尝试作为一个班级来做:

$traverseXML->getData("Route", "incoming", "field", "value")[0] 
//getData() returns an array, I'm trying to get a single value.
4

1 回答 1

0

我个人会做这样的事情

<?php 
function data($key = false, $default = 'not found') {
    $out['a'] = "abc";
    $out['b'] = "def";
    $out['c'] = "ghi";
    if($key)
    {
        if(isset($out[$key]))
            return $out[$key];
        else
            return $default;
    }       

    else
        return 'empty';
}
 ?>

 <?= data('a') ?>
于 2012-08-31T11:54:28.897 回答