简体   繁体   English

PHP为什么我的函数返回两次具有不同键的数组的结果?

[英]PHP why does my function return twice the result of the array with different keys?

I've a problem with one of my function in PHP. 我在PHP中的一个函数有问题。 It returns the result two times with different keys… 它使用不同的键返回结果两次...
I want the result only one time without the number keys. 我希望结果只有一次没有数字键。

This query and function returns the following array: 此查询和函数返回以下数组:

<?php
#The query
$typo = GetRowsParams("SELECT * FROM glyphs WHERE g_OK=1");  

#The function   
function GetRowsParams($requete, $params = array())
{
global $bdd;
$stmt = $bdd->prepare($requete) or die(print_r($req->errorinfo()));;
$stmt->execute($params);
$result = $stmt->fetchAll();
return $result;
}
?>


# The Array
Array (
[0] => Array (
    [g_ID] => 1
    [0] => 1
    [g_name] => zero_Nagar.svg
    [1] => zero_Nagar.svg
    [g_height] => 1174
    [2] => 1174
    [g_width] => 352
    [3] => 352
    [g_tag] => Test
    [4] => Test
    [g_u_ID] => 2
    [5] => 2
    [g_path] => 02uploads/Test/zero_Nagar.svg
    [6] => 02uploads/Test/zero_Nagar.svg
    [g_path_PNG] => 02uploads/Test/zero_Nagar.png
    [7] => 02uploads/Test/zero_Nagar.png
    [g_OK] => 1
    [8] => 1
    )
[1] => Array (
    [g_ID] => 2
    [0] => 2
    [g_name] => A
    Nagar.svg [1] => A
            …
            …

Why each row is displayed twice with a different key? 为什么每行用不同的键显示两次? Where is my mistake? 我的错误在哪里?

Thank you for your help… 谢谢您的帮助…

it is because by default php returns an array where all data has a textual AND a numeric index. 这是因为默认情况下,php返回一个数组,其中所有数据都有文本和数字索引。 To keep only the textual index, passe PDO::FETCH_ASSOC in the fechAll function like that : stmt->fetchAll(PDO::FETCH_ASSOC); 要仅保留文本索引,请在fechAll函数中传递PDO :: FETCH_ASSOC: stmt->fetchAll(PDO::FETCH_ASSOC);

Add the argument PDO::FETCH_ASSOC like so: 像这样添加参数PDO :: FETCH_ASSOC:

$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

(You can see the different fetch styles here: http://www.php.net/manual/en/pdostatement.fetch.php ) (你可以在这里看到不同的获取样式: http//www.php.net/manual/en/pdostatement.fetch.php

Read up on the PDOStatement::fetch methods . 阅读PDOStatement :: fetch方法 The first argument is a "fetch style," which defaults to fetching an array that looks like the above. 第一个参数是“获取样式”,默认为获取类似于上面的数组。 If you just want an array that maps field names to valuse, use: 如果您只想要一个将字段名称映射到valuse的数组,请使用:

$result = $bdd.fetchAll(PDO::FETCH_ASSOC);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 PHP array_udiff 返回不同的结果集取决于回调 function 是否返回 1 vs -1? - Why does PHP array_udiff return a different result set depending on whether the callback function returns 1 vs -1? 为什么这个PHP函数返回错误的结果? - Why does this PHP function return the wrong result? 为什么用数组引用调用PHP函数的这种较短方法导致不同的结果? - Why does this shorter way of calling a PHP function with an array reference lead to a different result? 为什么我的PHP CURL POST返回空结果 - Why does my PHP CURL POST return a null result 为什么查询数组会返回PHP中的对象数组? - Why does my query to array return an array of objects in PHP? PHP-OOP-为什么我的函数被调用两次? - PHP - OOP - Why is my function called twice? PHP中的函数不返回数组的值,为什么? - Function in PHP doesn't return the value of my Array, why? 为什么我的 function 返回数组中除最后一项以外的所有内容? - Why does my function return everything except the last item in the array? PHP函数不返回数组 - PHP function does not return array 为什么通过函数包含的PHP返回的结果与主文件中的PHP不同? - Why would PHP included via a function return a different result from the PHP in the main file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM