这就是您要寻找的.
一些见识-您只看到一个字符,因为您没有多维数组.您有一个一维字符串数组,当您访问这些字符串的索引时,您会在字符串中得到一个字符.
您需要代表数组的键的关联数组.
我添加了其他代码,以使用IN子句将您的搜索连接到单个搜索中,因此它将执行WHERE类别IN(“ Alpha”,“ Beta”等)之类的操作.这样会更快.
最后,对于每个记录,您要将其添加到具有匹配索引的category数组中.这会将您的数据分类到类别集合中.
哦,也不需要行计数器.只需将行添加到其类别的末尾,即可在数组中对其进行正确索引.
$category = array(
"Alpha" => array(),
"Beta" => array(),
"Gamma" => array(),
"Delta" => array(),
"Epsilon" => array(),
"Zeta" => array()
);
// Concatenate the categories for searching
$categories = array_keys($category);
for ($i = 0; $i < count($categories); $i++) {
$categories[$i] = "'{$categories[$i]}'";
}
$categories = implode(",", $categories);
// Query on an IN clause
$query = "SELECT * FROM random_walk WHERE category IN ({$categories})";
$result = $mysqli->query($query) or die($mysqli->error . __LINE__);
$row_cnt = $result->num_rows;
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$category[$row['category']][] = $row['image_filename'];
echo $category[$row['category']][count($category[$row['category']]) - 1];
}
}