【发布时间】:2015-06-22 07:11:00
【问题描述】:
我有一个关联数组。如果值首先出现,我想删除重复值。假设如果DC-30 首先出现,则删除关联数组中的所有 DC-30。如果HSD-M 已经出现,则删除关联数组中的所有HSD-M。如果这有什么不同,我会在while($row = mysql_fetch_assoc($result)) 中完成所有这些工作。这里有什么帮助吗?
string(5) "DC-30"
string(5) "DC-30"
string(10) "GigaDigHD2"
string(11) "Turbo-Frame"
string(5) "HSD-M"
string(5) "HSD-M"
string(5) "HSD-M"
string(5) "HSD-M"
编辑:这些数组值不是硬编码的。它们来自数据库中的表,这就是我要删除重复值的原因。
想要的输出是:
string(5) "DC-30"
string(10) "GigaDigHD2"
string(11) "Turbo-Frame"
string(5) "HSD-M"
第二次编辑:
while($row = mysql_fetch_assoc($result))
{
$tester_name = $row['tester_name'];
$board_name = $row['board_name'];
$config = $row['config'];
$req_config = $row['configuration'];
echo $bomb = array_unique($board_name, SORT_REGULAR); //Edited here
$ex_req_config = explode(",", $req_config);
$count_req_config = count($ex_req_config);
$match = 0;
foreach($ex_req_config as $value)
{
$number = strlen($value);
$arr = str_split($value, $number);
if(in_array($config, $arr))
{
$match += 1;
$match /= ($count_req_config / 100);
}
}
/* echo "<tr class = \"thisRow\">
<td></td>
<td></td>
<td class = \"match\">$match%</td>
</tr>";*/
}
【问题讨论】:
-
添加支票。只存入数组一次。
-
array_unique($your_array,SORT_REGULAR) -
@b0s3 不知道如何只做一次存储数组。这些值从表中查询并存储到具有多个值的关联数组中。
-
那么宇智波6是对的。使用他的建议
-
大多数情况下,这是在查询数据库时可以做的事情;即只向数据库询问唯一值。这对你来说是一个选择吗?
标签: php arrays string associative-array