mysqli_fetch_array() foreach loop duplicates

后端 未结 1 1302
野的像风
野的像风 2021-01-03 09:49

I\'m performing a request on a database which returns an array. I\'m using a foreach loop to go through the values.

Database

相关标签:
1条回答
  • 2021-01-03 10:27

    By default, mysqli_fetch_array() returns both an Associative Array ($key => $value) and a Numeric Array (0 => value), which is why you're getting duplicates - some of the entries have the column name as the key, the others have a numeric index.

    If you want an associative array, you could use:

     mysqli_fetch_array($q, MYSQLI_ASSOC);
     //or
     mysqli_fetch_assoc($q);
    

    Or for a numeric array:

     mysqli_fetch_array($q, MYSQLI_NUM);
     //or
     mysqli_fetch_row($q);
    

    For more info: http://www.php.net/manual/en/mysqli-result.fetch-array.php

    0 讨论(0)
提交回复
热议问题