php Mysqli fetch_assoc 与 fetch_array
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21361184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mysqli fetch_assoc vs fetch_array
提问by Darren Sweeney
When I'm returning one row from a table, to gather the results I usually use e.g.:
当我从表中返回一行时,为了收集我通常使用的结果,例如:
$info = $result->fetch_assoc();
What is the difference between that and:
那和有什么区别:
$info = $result->fetch_array();
Is there a reason to use one over the other when returning only one row, or is it just personal preference?
当只返回一行时,是否有理由使用一个而不是另一个,还是只是个人喜好?
回答by Mr. Alien
It's all about performance
一切都与性能有关
fetch_array()returns one array with both numeric keys, and associative strings (column names), so here you can either use $row['column_name']or $row[0]
fetch_array()返回一个同时包含数字键和关联字符串(列名)的数组,因此在这里您可以使用$row['column_name']或$row[0]
Where as fetch_assoc()will return string indexed key array and no numeric array so you won't have an option here of using numeric keys like .$row[0]
asfetch_assoc()将返回字符串索引键数组,而没有数字数组,因此您将无法选择使用像.$row[0]
So the latter one is better in performance compared to fetch_array()and obviously using named indexes is far better compared to numeric indexes.
因此,fetch_array()与使用命名索引相比,后者在性能上要好得多,显然使用命名索引比使用数字索引要好得多。
回答by Mohammad Shahnewaz Sarker
fetch_array returns value with indexing. But Fetch_assoc just returns the the value.
fetch_array 返回带索引的值。但是 Fetch_assoc 只是返回值。
for example fetch_array returns
例如 fetch_array 返回
[0] => 11
[id] => 11
[1] => 2014-12-29
[date] => 2014-12-29
here array location 0 contains 11 also this location name is 'id'.
这里数组位置 0 包含 11 这个位置名称是“id”。
same things fetch_assoc will returns
fetch_assoc 会返回同样的东西
[id] => 11
[date] => 2014-12-29
means just returns the value.
意味着只返回值。
回答by user960401
(PHP 5) From: http://php.net/manual/en/mysqli-result.fetch-array.php
(PHP 5)来自:http: //php.net/manual/en/mysqli-result.fetch-array.php
mysqli_result::fetch_array -- mysqli_fetch_array — Fetch a result row as an associative, a numeric array, or both
mysqli_result::fetch_array -- mysqli_fetch_array — 获取结果行作为关联数组、数字数组或两者兼而有之
So effectively, fetch_array()and fetch_assoc()can be essentially equivalent calls. As for performance, I recommend using whichever call that results in more readable code and only be concerned about the performance after you've clearly identified the call as a performance issue. It's more likely that you've got more serious performance issues where you least expect them to be.
如此有效,fetch_array()并且fetch_assoc()可以本质上是等效的调用。至于性能,我建议使用导致代码可读性更高的调用,并且只有在您明确将调用确定为性能问题后才关心性能。您更有可能在最不希望出现的地方遇到更严重的性能问题。
回答by Pinky
fetch_arraywill give you key/value pairs and with indexes, where as fetch_assocwill give you only the key/value pairs but not with indexes. For example:
fetch_array会给你键/值对和索引,而 asfetch_assoc只会给你键/值对而不是索引。例如:
//fetch_array output be like:
[name]=>'someName'
[0]=>'someName'
[email]=>'[email protected]'
[1]=>'[email protected]'
//fetch_assoc output be like
[name]=>'someName'
[email]=>'[email protected]'

