PHP:如何识别和数组中的更改,重复值? [英] PHP: How to identify AND CHANGE duplicate values in an array?

查看:210
本文介绍了PHP:如何识别和数组中的更改,重复值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK,有很多的重复检测和PHP数组拆除的例子,使用array_unique()等,但如果你想找到DUP的内容,修改它们,在一个循环中再次检查,直到所有的DUP现在唯一<? / p>

我觉得它像使用array_filter()...所以作为一个更具体的例子,这里是会来的sql语句是这样的:

  SELECT标识,list.comboname
FROM列表
   内部联接 (
      SELECT comboname
      FROM列表
       GROUP BY comboname
       HAVING计数(ID)&GT; 1
   )DUP ON list.comboname = dup.comboname

要重复的表中的一个数组:

 阵列(
    [0] =&GT; 49
    [1] =&GT; big.dup
    [2] =&GT; 233
    [3] =&GT; another.duplicate
    [4] =&GT; 653
    [5] =&GT; big.dup
    [6] =&GT; 387
    [7] =&GT; big.dup
    [8] =&GT; 729
    [9] =&GT; another.duplicate
    [10] =&GT; 1022
    [11] =&GT; big.dup

现在我要的是一些PHP删除字符直到周期,使他们独特的[或在必要的末尾添加数字]

所以,结果将是:

 阵列(
    [0] =&GT; 49
    [1] =&GT; big.dup
    [2] =&GT; 233
    [3] =&GT; another.duplicate
    [4] =&GT; 653
    [5] =&GT; big.du
    [6] =&GT; 387
    [7] =&GT; big.d
    [8] =&GT; 729
    [9] =&GT; another.duplicat
    [10] =&GT; 1022
    [11] =&GT; big.dup1

在保留原有的值(即big.dup和another.duplicate)......我已经经历几乎每个PHP数组函数看上去试图想象战略...想法?


解决方案

对于你的问题阵列和在若重复的末尾添加数字,你只需要循环数组过一次,并临时建立一个帮手阵列如果存储的价值已经被发现(和频率):

  $ =发现阵列();的foreach($数组和放大器; $值)
{
    如果(is_int($值))继续; #跳过整数值    如果(使用isset($找到[$值))
    {
        $值= sprintf的(%S-%D',$价值$ ++发现[$值]);
    }
    其他
    {
        $发现[$值] = 0;
    }
}
未设置($值);

演示

OK, there are a lot of examples of duplicate detection and removal in php arrays, using array_unique() etc but what if you want to find dups, modify them, check again in a loop until all dups are now unique?

I think it's something like using array_filter()... so as a more specific example, here's what would come out of a sql statement something like this:

SELECT id, list.comboname 
FROM list
   INNER JOIN (
      SELECT comboname 
      FROM list
       GROUP BY comboname 
       HAVING count(id) > 1
   ) dup ON list.comboname = dup.comboname

To an array of the duplicates in the table:

Array ( 
    [0] => 49 
    [1] => big.dup  
    [2] => 233  
    [3] => another.duplicate  
    [4] => 653  
    [5] => big.dup  
    [6] => 387  
    [7] => big.dup  
    [8] => 729  
    [9] => another.duplicate  
    [10] => 1022  
    [11] => big.dup   
)

Now what I want is some PHP to delete characters until the period so they are unique [or add numbers if needed to the end]

So result would be:

Array (  
    [0] => 49  
    [1] => big.dup  
    [2] => 233  
    [3] => another.duplicate  
    [4] => 653  
    [5] => big.du  
    [6] => 387  
    [7] => big.d  
    [8] => 729  
    [9] => another.duplicat  
    [10] => 1022  
    [11] => big.dup1  
)

While retaining the original value (i.e. big.dup and another.duplicate)... I've looked through just about every PHP array function trying to imagine a strategy ... ideas?

解决方案

For the array in your question and for adding numbers at the end if a duplicate, you only need to loop over the array once and temporary build up a helper array that stores if a value has been already found (and how often):

$found = array();

foreach($array as &$value)
{
    if (is_int($value)) continue; # skip integer values

    if (isset($found[$value]))
    {
        $value = sprintf('%s-%d', $value, ++$found[$value]);
    }
    else
    {
        $found[$value] = 0;
    }
}
unset($value);

Demo

这篇关于PHP:如何识别和数组中的更改,重复值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆