php - PHP 中是否有内置方法可以从多维数组中获取数组?

标签 php arrays pdo flatten

(查询数据库时非常有用)。
如果我有一个多暗淡数组

 [['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/

相关文章:

php - mySQL UPDATE TEXT 与 INT 字段类型(速度性能)

mysql - 准备好的语句相对于 PDO::quote 的安全优势

PHP PDO 与 BindValue

php - 在函数内调用 PDO

javascript - 数据作为对象和数组传递?

php - CURLOPT_POST 与 CURLOPT_POSTFIELDS : Is CURLOPT_POST option required?

c - 如何在C中解析数组

c - 在 C 中,数组定义中的长度如何映射到寻址?

php - "session.save_path"中php session 文件的最大数量是多少

c - 如何在数组中查找具有相同值的连接单元格