为什么我不能在函数的返回值上使用数组索引?

发布于 2024-11-14 01:07:54 字数 237 浏览 4 评论 0原文

为什么我不能这样做?

explode(',','1,2,3', 1)[0]

所有其他语言都支持它。

我正在寻找的答案:(因为人们似乎认为这是毫无意义的咆哮)

我应该意识到变量和返回值的行为方式之间是否存在某种差异?

这是一个设计决定吗?他们只是懒惰吗?该语言的构建方式是否存在某种原因导致其实现起来极其困难?

Why can't I do this?

explode(',','1,2,3', 1)[0]

Every other language supports it.

Answers I'm looking for: (since people seem to think this is a pointless rant)

Is there some sort of a difference between how a variable and a return value behaves that I should be made aware of?

Was this a design decision? Were they just lazy? Is there something about the way the language was built that makes this exceedingly difficult to implement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倾城°AllureLove 2024-11-21 01:07:54

为什么我不能这样做?

因为PHP目前不支持这种语法。但你可以将它传递给一个函数,例如:

current(explode(',','1,2,3', 1));

否则,你必须先将返回值分配给一个变量,然后通过索引访问。

更多信息

因此可以链接方法调用或
财产访问。现在很长一段时间
人们要求同样的事情
数组偏移访问。 这经常发生
由于不确定性而被拒绝
内存问题
,因为我们不喜欢
内存泄漏。但经过适当的
评价 Felipe 提交的补丁
它允许您执行以下操作:

function foo() {
    return array(1, 2, 3);
}
echo foo()[2]; // prints 3

http:// /schlueters.de/blog/archives/138-Features-in-PHP-trunk-Array-dereferencing.html

所以,它正在开发中,但正如我所说,目前还没有支持。

更新

PHP >= 5.4 现在支持函数数组解引用,例如foo()[0]

Why can't I do this?

Because PHP doesn't currently support this syntax. But you can pass it to a function, e.g.:

current(explode(',','1,2,3', 1));

Otherwise, you have to assign the return value to a variable first, then access via index.

More Info:

So one can chain method calls or
property access. Now for a long time
people requested the same thing for
array offset access. This was often
rejected due to uncertainties about
memory issues
, as we don't like
memory leaks. But after proper
evaluation Felipe committed the patch
which allows you to do things like:

function foo() {
    return array(1, 2, 3);
}
echo foo()[2]; // prints 3

http://schlueters.de/blog/archives/138-Features-in-PHP-trunk-Array-dereferencing.html

So, it is in the development pipeline but, as I stated, is not currently supported.

Update

PHP >= 5.4 now supports function array dereferencing, e.g. foo()[0]

同尘 2024-11-21 01:07:54

在我的框架中,我编写了一个函数,能够对所有可能的数组执行此操作:

function elem($array,$key) {
  return $array[$key];
}

//> usage 
echo elem($whateverArrayHere,'key');
echo elem(explode(),1);

In my framework I wrote a function to be able to do it with all possiible array:

function elem($array,$key) {
  return $array[$key];
}

//> usage 
echo elem($whateverArrayHere,'key');
echo elem(explode(),1);
凉墨 2024-11-21 01:07:54

爆炸返回一个数组,而不是引用。

$tab = explode (',','1,2,3', 1);
$tab[0] = ','

Explode return an array, not reference.

$tab = explode (',','1,2,3', 1);
$tab[0] = ','
风尘浪孓 2024-11-21 01:07:54

您也可以考虑......

  list($value_I_want_to_keep)=explode(',','1,2,3', 1);

  $value_I_want_to_keep=strtok('1,2,3',',');

You might also consider....

  list($value_I_want_to_keep)=explode(',','1,2,3', 1);

or

  $value_I_want_to_keep=strtok('1,2,3',',');
煞人兵器 2024-11-21 01:07:54
$pieces = explode(',','1,2,3', 1);

使用第一个索引:

$pieces[0];
$pieces = explode(',','1,2,3', 1);

Use the first index with:

$pieces[0];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文