[英]PHP Array duplicates: every string key has numeric key
I'm trying to get an array with utf-8 values from an MS-SQL query. 我正在尝试从MS-SQL查询中获取具有utf-8值的数组。
Nearly everything works fine, except that the result array looks like this: 几乎一切都运行正常,除了结果数组看起来像这样:
Array (
[id] => 1;
[0] => 1; // this is redundant
[countryName] => england;
[1] => england; // this is redundant
)
I don't want the duplicate numeric keys. 我不想要重复的数字键。 Why are they even created?
为什么他们甚至创造了? The code which is leading to this result is:
导致此结果的代码是:
# execute the query
foreach ($pdoConnection->query($sqlStatement) as $row) {
// encode row in utf8 so json works and save row in array
$output[] = array_map('utf8_encode', $row);
}
Thanks for any idea how that can be solved. 感谢您知道如何解决这个问题。
You need to fetch as associative
. 你需要以
associative
方式获取。 Follow this example: 请遵循以下示例:
$stmt = $this->db->prepare('SELECT title, FMarticle_id FROM articles WHERE domain_name =:domain_name');
$stmt->bindValue(':domain_name', $domain);
$stmt->execute();
$article_list = $stmt->fetchAll(PDO::FETCH_ASSOC);
Reference: PDOStatement::fetchAll 参考: PDOStatement :: fetchAll
Try replacing this line, 尝试更换此行,
foreach ($pdoConnection->query($sqlStatement, PDO::FETCH_ASSOC) as $row)
That flag is for getting associative records. 该标志用于获取关联记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.