我有一些数据存储在$cache[]中,其中有一些数字。如何删除打印输出中的重复值?:
<?
#mysql connect
mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_select_db($name) or die(mysql_error());
function get_numerics ($str) {
preg_match_all('/\d+/', $str, $matches);
return $matches[0];
}
function validatecard($number) {
//preg_match_all('/^[6-9][0-9]{9}$/', $number, $found);
preg_match_all('/^[0-9]{4}$/',$number, $found);
//print_r($found);
return $found[0];
}
$query = mysql_query("SELECT * FROM client WHERE status = 1 ORDER BY id")
or die(mysql_error());
while ($raw = mysql_fetch_array($query))
{
$name = $raw["postdata"];
$id = $raw["id"];
$cache = [];
for($i=0;$i<=count(get_numerics($name));$i++){
$ccv = get_numerics($name);
$num = $ccv[$i];
if (is_numeric($num)) {
$cc = validatecard($num);
if (!in_array($cc, $cache)){
$cache[] = $cc;
}
}
}
print_r($cache);
}
?>我使用了一些函数,比如:数组唯一,并将其转换为json或serialize…而不是工作。
发布于 2018-03-21 21:05:28
使用array_unique($array)函数。
它删除数组的重复值。
有关更多信息,请查看以下内容:
http://php.net/manual/en/function.array-unique.php
发布于 2018-03-21 21:09:00
如果$cache是多维的,那么array_unique将无法工作。您可能想要使用:
$input = array_map("unserialize", array_unique(array_map("serialize", $input)));请参阅:How to remove duplicate values from a multi-dimensional array in PHP
发布于 2018-03-21 21:11:04
或者让数据库使用DISTINCT来处理去重
SELECT
DISTINCT
id
, postdata
FROM
client
WHERE
status = 1
ORDER BY
id ASChttps://stackoverflow.com/questions/49407050
复制相似问题