print_r redundencie(索引和键值)

时间:2016-10-12 15:38:22

标签: php arrays indexing

我的PDO查询后

$reponse=$bdd->query("SELECT * FROM coordonnees WHERE id=9); 
$data=$reponse->fetch()

我print_r $ data

我有类似

的东西
[id] => 70
[0] => 70
[nom] => Nom
[1] => Nom
[rue] => rue de Joie, 144
[2] => rue de Joie, 144
[numero] => 
[3] => 
[bte] => 
[4] => 
[codep] => 5000
[5] => 5000
[ville] => Namur
[6] => Namur
[pays] => Belgique
[7] => Belgique
[email] => xxxxt@xxxx.net
[8] => xxxx@xxxx.net
[tel] => 0xxx/42.48.72
[9] => 0xxx/42.48.72

因此,由于索引,所有内容都是双倍的。是什么原因以及如何避免这种情况以获得

[id] => 70
[nom] => Nom
[rue] => rue de Joie, 144
[numero] => 
[bte] => 
[codep] => 5000
[ville] => Namur
[pays] => Belgique
[email] => xxxxt@xxxx.net
[tel] => 0xxx/42.48.72

非常感谢你!

2 个答案:

答案 0 :(得分:0)

应该使用默认->fetch()

但是,您可以使用参数

定制所需的输出

要获取返回的关联数组,请执行

$reponse=$bdd->query("SELECT * FROM coordonnees WHERE id=9"); 
----------------------------------------------------------^
$data=$reponse->fetch(PDO::FETCH_ASSOC);

或者如果你想要一个对象返回

$reponse=$bdd->query("SELECT * FROM coordonnees WHERE id=9"); 
$data=$reponse->fetch(PDO::FETCH_OBJ);
  

请参阅the manual了解所有可能性

答案 1 :(得分:0)

阅读文档! http://php.net/manual/en/pdostatement.fetch.php

它的获取方式,

while True:
    a1  = raw_input('Enter concentration of hydrogen (in decimal form): ')
    a2 = raw_input('Enter concentration of chlorine (in decimal form): ')
    a3 = raw_input('Enter concentration of calcium (in decimal form): ')
    try:
        a1 = float(a1)
        a2 = float(a2)
        a3 = float(a3)
        break
    except ValueError:
        print('All inputs must be numerals')

是默认

PDO::FETCH_BOTH (default): returns an array indexed by both column name and 0-indexed column number as returned in your result set

你需要的是代码

PDO::FETCH_ASSOC: returns an array indexed by column name as returned in your result set

即使在文档中的示例中也是如此!