[英]Array output format in php
I am running SQL select query,getting the result in below format after executing the query. 我正在运行SQL选择查询,执行查询后以以下格式获取结果。
Array
(
[0] => Array
(
[usertype_id] => 14
)
[1] => Array
(
[usertype_id] => 15
)
[2] => Array
(
[usertype_id] => 17
)
)
But i need result in below format 但我需要以下格式的结果
Array
(
[0] => 14
[1] => 15
[2] => 17
)
So how to loop through this to get the output in above format. 因此,如何循环遍历以获取上述格式的输出。
array_column works just fine here: array_column在这里可以正常工作:
https://3v4l.org/qX46k https://3v4l.org/qX46k
<?php
$input = [['usertype_id' => 14], ['usertype_id' => 15], ['usertype_id' => 17]];
$expected = [14,15,17];
$result = array_column($input, 'usertype_id');
var_dump($result === $expected);
Output for 7.1.25 - 7.3.2 7.1.25-7.3.2的输出
bool(true) 布尔(真)
use array array_map()
使用数组array_map()
$res= array_map(function($arr=array()){
return $arr['usertype_id'];
},$input);
print_r($res);
If you are using PDO, then you can do as below 如果您使用的是PDO,则可以执行以下操作
<?php
$sth = $dbh->prepare("SELECT usertype_id FROM user");
$sth->execute();
/* Fetch all of the values of the first column */
$result = $sth->fetchAll(PDO::FETCH_COLUMN, 0);
print_r($result);
?>
Reference: http://php.net/manual/en/pdostatement.fetchall.php 参考: http : //php.net/manual/en/pdostatement.fetchall.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.