我有以下的PHP/MySQL查询:转换多维数组到一维使用值从子阵列
$sql = SELECT type, count(*) AS total FROM stats GROUP BY type
$this->sql = $sql;
function numObjects() {
$stats = $this->conn->query($this->sql);
while($stat_type = $stats->fetch_assoc()){
$category[] = $stat_type;
}
return $category;
}
这是一个简单的查询,但我无法弄清楚如何检索数据的方式我想要,因为它正在返回一个多维数组。
array(2) {
[0]=> array(2) {
["type"]=> string(4) "page"
["total"]=> string(1) "1"
}
[1]=> array(2) {
["type"]=> string(8) "category"
["total"]=> string(1) "1"
}
}
我正在做的是将每个数组的值转换为key => value对。像这样:
["page"]=> "1"
["category"]=> "1"
我已经能够将它转换为一维数组,但不是正确的格式。
这工作完美。谢谢@heychez! – EternalHour 2014-09-10 23:15:56