【问题标题】:Strange behaviour of an array returned by a function [duplicate]函数返回的数组的奇怪行为[重复]
【发布时间】:2013-05-31 11:46:42
【问题描述】:

为什么这段代码有效:

$row = $stmt->fetch();
$result = array("status"=>"0", "uid"=>($row["id"]));

而这个没有:

$result = array("status"=>"0", "uid"=>(($stmt->fetch())["id"]));

【问题讨论】:

  • 抱歉重复,感谢您的回答。
  • @ValentinSolina 没问题。感谢 15 位代表接受 ;)

标签: php mysql pdo


【解决方案1】:

从 PHP 5.4 开始,您可以按照自己的方式进行操作。

getSomeArray()[$someKey]

参考:http://php.net/manual/en/language.types.array.php#example-88

在 PHP 5.3 之前,您需要使用临时变量。

【讨论】:

  • 酷,我不知道他们在 PHP 5.4 中添加了这个。
  • @MattBrowne 我知道,它很有用
  • 为什么人们一直反对这个?在 PHP 5.3 或更低版本中,临时变量确实是实现此目的的唯一方法。
  • @MattBrowne 我快要爆炸了。当我看到那个大三时,我很高兴......
  • @Vivek 你不明白吗?加入俱乐部:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-26
  • 1970-01-01
  • 2021-04-08
  • 1970-01-01
  • 2017-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多