English 中文(简体)
PHP: How to identifying and CHANGE pornography in a range?
原标题:PHP: How to identify AND CHANGE duplicate values in an array?

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?

我认为,它像使用阵列(过滤器)一样,是一个更具体的例子,在这里,它会引出象这样一句 statement语的话:

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

表中的一系列重复:

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   
)

现在,我想要的是一些PHP,以便在这段时期之前删除其特性,以便它们具有独特性[或者如果需要的话,就添加数字]。

结果是:

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  
)

保留原有价值(即大宗和另一宗)。 页: 1 我只想看一下每一个PHP阵列功能,试图想象一项战略......想法?

问题回答

对于你提出的问题中的阵列,如果重复的话,为了最终增加数字,你只需要一劳永逸地建造一只助手阵列,如果已经找到价值的话,就储存起来(而且经常):

$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);

http://codepad.viper-7.com/1fHbov” rel=“nofollow”>

首先,我认为你的结构非常复杂。

为什么不要把它变成像:

$arr = array(
     49  =>  big.dup ,
     233  =>  another.duplicate ,
     653  =>  big.dup ,
     387  =>  big.dup ,
     729  =>  another.duplicate ,
     1022  =>  big.dup ,
);

这样,你就能够方便地检查重复使用诸如:

$arr = array(
     49  =>  big.dup ,
     233  =>  another.duplicate ,
     653  =>  big.dup ,
     387  =>  big.dup ,
     729  =>  another.duplicate ,
     1022  =>  big.dup ,
);
$arr_val = array();
foreach( $arr as $key => $val)
{
    if(isset($arr_val[ $val ]))
    {
        $arr_val[ $val ]++;
        $arr[ $key ] = $val . $arr_val[ $val ];
    }
    else
    {
        $arr_val[ $val ] = 0;
    }
}

或者,如果你坚持使用这种复杂的阵列结构,你可以修改上述准则:

$arr_val = array();
foreach( $arr as $key => $val)
{
    if(isset($arr_val[ $val ]) && !is_numeric( $val ) )
    {
        $arr_val[ $val ]++;
        $arr[ $key ] = $val . $arr_val[ $val ];
    }
    else
    {
        $arr_val[ $val ] = 0;
    }
}

你可以指出,这里没有什么不同。 我仅加上<代码>和&!is_numeric($val),因为我认为,你不想处理该身份证。 尽管我仍然认为,该身份证永远不会重复。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签