(查询数据库时非常有用)。
如果我有一个多暗淡数组
[['id'=>1],['id'=>2],['id'=>34],['id'=>67]]
我想要的是[1,2,34,67]
我知道如何在代码中执行此操作,只是询问 PHP 中(或者可能在 PDO 中)是否有内置方法来执行此操作。
最佳答案
我认为你需要PDO::FETCH_COLUMN
模式为fetchAll
,它仅返回单个列作为数组:
<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* Fetch all of the values of the first column */
$result = $sth->fetchAll(PDO::FETCH_COLUMN, 0);
var_dump($result);
?>
来自the manual :
To return an array consisting of all values of a single column from the result set, specify PDO::FETCH_COLUMN. You can specify which column you want with the column-index parameter.
关于php - PHP 中是否有内置方法可以从多维数组中获取数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2208508/