读取 PHP 数组的第一项
我有一个由以下代码生成的 PHP 数组:
$sql = "SELECT adresa.Id_Adresa as id FROM adresa";
$result = $this->os->db->conn->query($sql);
$data = array();
while ($r = $result->fetch(PDO::FETCH_ASSOC)) {
$data[] = $r;
}
我想获取该数组的第一个 id。我怎样才能在不解析所有项目的情况下做到这一点?提前致谢。
I have a PHP array generated by the following code:
$sql = "SELECT adresa.Id_Adresa as id FROM adresa";
$result = $this->os->db->conn->query($sql);
$data = array();
while ($r = $result->fetch(PDO::FETCH_ASSOC)) {
$data[] = $r;
}
I want to grab the very first id of this array. How can I do that without parsing all the items? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您说
此数组的第一个 id
时,我假设它是$data
数组,只需执行以下操作:When you say
first id of this array
I am assuming that is the$data
array, just do:如果不需要,为什么不限制退回物品的数量呢?
Why don't you limit the number of items returned if you don't need them?
请参阅 https://www.php.net/manual/en/ 上的文档pdostatement.fetchcolumn.php
See the docs at https://www.php.net/manual/en/pdostatement.fetchcolumn.php