php爆炸 - 需要第二个元素

时间:2013-06-12 04:53:42

标签: php arrays object get indexing

更多的是感兴趣...

$_GET['unique'] = blahblahblah=this_is_what_im_interested_in

我知道我可以得到第二个这样的元素:

$words = explode('=', $_GET['unique']);
echo $words[1];

有没有办法在一行中获得这个? - 然后'希望'允许我将其添加到函数/对象调用:

$common->resetPasswordReply(... in here I would put it....);

喜欢

$common->resetPasswordReply(explode('=', $_GET['unique'])[1]);

我只是想知道这是否可能。

3 个答案:

答案 0 :(得分:7)

PHP支持函数索引,如果它们返回数组/对象;以下也适用:

echo explode('=', $_GET['unique'])[1];

修改

这被称为array dereferencing,已在PHP文档中介绍过:

  

从PHP 5.4开始,可以对a的结果进行数组解除引用   函数或方法直接调用。之前它只能使用一个   临时变量。

     

从PHP 5.5开始,可以对数组取消引用数组文字。

答案 1 :(得分:1)

这应该为你做。

substr($str, strpos($str, "=")+1);

答案 2 :(得分:1)

奇怪的是,你几乎可以用list()来做这件事,但你不能在函数调用中使用它。我只发帖说你'more out of interest': -

$_GET['unique'] = "blahblahblah=this_is_what_im_interested_in";

list(, $second) = explode('=', $_GET['unique']);

var_dump($second);

输出: -

string 'this_is_what_im_interested_in' (length=29)

您可以在manual page的第一组示例中看到有关list()的灵活性的良好示例。

我认为值得指出的是,虽然你的例子可行: -

$common->resetPasswordReply(explode('=', $_GET['unique'])[1]);

它会使您的代码变得模糊,并且您传递给函数的内容并不明显。而类似以下内容的内容更具可读性: -

list(, $replyText) = explode('=', $_GET['unique']);
$common->resetPasswordReply($replyText));

考虑在6个月后回到您的代码并尝试调试它。使其尽可能自我记录。此外,不要忘记,当您在此处接收用户输入时,需要在某些时候对其进行消毒。