我正在尝试编写一个脚本,该脚本从 SQL 查询中获取结果,并在遍历每个结果时检查 $row[13] 的名称应该是什么,然后我想要遍历它识别的每个查询结果任何相似的名称,并记录它看到该名称的次数。我以为我有一个循环计算出相同名称在数组中出现的次数,然后显示它。我正在寻找的最终结果是
Operations - 87
Training - 32
HR - 12
但是在我运行这段代码之后:
while($row = mysql_fetch_array($result_id))
{
$profile8 = mysql_query
("
SELECT org
FROM cost_centers
WHERE cost_center = '$row[13]'
")
or die(mysql_error());
$anymatches=mysql_num_rows($profile8);
while($pro8 = mysql_fetch_array($profile8))
{
$orgnames = $pro8[0];
}
$names[] = $orgnames;
$newArray = array_count_values($names);
foreach ($newArray as $key => $value) {
echo "$key - $value <br />";
}
}
我最终会像这样不断地计算它:
HR - 1
HR - 1
Communications - 1
HR - 1
Communications - 1
- 1
HR - 1
Communications - 1
- 2
HR - 1
Communications - 1
- 3
HR - 1
Communications - 1
- 4
HR - 2
Communications - 1
- 4
HR - 3
Communications - 1
- 4
HR - 3
Communications - 1
- 4
Operations - 1
HR - 4
Communications - 1
- 4
Operations - 1
HR - 4
Communications - 1
- 5
Operations - 1
HR - 4
Communications - 1
- 6
Operations - 1
HR - 4
Communications - 1
- 6
Operations - 2
HR - 4
Communications - 1
- 6
Operations - 3
HR - 4
所以它看起来就像是先分组然后计数,然后在不断增加计数时重新分组。我希望这是有道理的,并提前感谢。