<?php
// $mysqli is already declared as a working database connection by this sandbox
$sql = <<<SQL
SELECT model,
storage,
color,
COUNT(*) count
FROM stock
GROUP BY model,
storage,
color
SQL;
$result = [];
foreach ($mysqli->query($sql) as $row) {
extract($row);
$result[$model][$storage][$color] = (int) $count;
}
var_export($result);