doushou6480 2017-03-08 11:12
浏览 58
已采纳

PHP数组重复:每个字符串键都有数字键

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

Thanks for any idea how that can be solved.

  • 写回答

2条回答 默认 最新

  • dongmubi4444 2017-03-08 11:16
    关注

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

    Reference: PDOStatement::fetchAll

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料