PHP数组重复项:每个字符串键都有数字键 [英] PHP Array duplicates: every string key has numeric key

查看:128
本文介绍了PHP数组重复项:每个字符串键都有数字键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从MS-SQL查询中获取具有utf-8值的数组.

I'm trying to get an array with utf-8 values from an MS-SQL query.

几乎所有的东西都可以正常工作,除了结果数组看起来像这样:

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);
}

感谢您知道如何解决.

推荐答案

您需要将其提取为associative.请遵循以下示例:

You need to fetch as 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);

参考: PDOStatement :: fetchAll

这篇关于PHP数组重复项:每个字符串键都有数字键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆