I have the following PHP/MYSQL query:
$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;
}
It's a simple query, but I just can't figure out how to retrieve the data the way I want since it is returning a multidimensional array.
array(2) {
[0]=> array(2) {
["type"]=> string(4) "page"
["total"]=> string(1) "1"
}
[1]=> array(2) {
["type"]=> string(8) "category"
["total"]=> string(1) "1"
}
}
What I'm looking to do is convert the values from each array, into key => value pairs. Like this:
["page"]=> "1"
["category"]=> "1"
I've been able to convert it into a single dimensional array, but not in the correct format.
try this:
$new_array = array();
foreach ($old_array as $elem) {
$new_array[$elem["type"]] = $elem["total"];
}
var_dump($new_array); //for print the array
0 comments:
Post a Comment