Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 更新列为重复项添加后缀_Php_Mysql - Fatal编程技术网

Php 更新列为重复项添加后缀

Php 更新列为重复项添加后缀,php,mysql,Php,Mysql,列标题如下所示: gold blue night silver morning about earth sun vs king blue night 因此存在重复项(在上述情况下-bluenight) 我需要重新命名所有副本,添加-02,-03。。。最后 例如,第二个blue-night应该是blue-night-02 如果有第三个blue night,则应该是blue-night-03,依此类推 有什么帮助吗?使用数组项作为计数器非常简单(可能不是最复杂的方法,但可能): 使用array\

标题
如下所示:

gold
blue night
silver morning
about earth 
sun vs king
blue night
因此存在重复项(在上述情况下-
bluenight

我需要重新命名所有副本,添加
-02
-03
。。。最后

例如,第二个
blue-night
应该是
blue-night-02

如果有第三个
blue night
,则应该是
blue-night-03
,依此类推


有什么帮助吗?

使用数组项作为计数器非常简单(可能不是最复杂的方法,但可能):


使用
array\u count\u values()更简洁的方法


演示:

您有任何代码片段吗?你在php上需要它吗?@don'tangryme,是的,我在php中需要它。然后用标题发布你的数组。我添加了一个较短版本的答案。希望它有帮助:)有一个很好的外观,谢谢你,还有如何用新值更新列
标题
?在主键上用一个简单的SQL update语句?不知道,您还没有提供表结构。
Array ( 
  [0] => gold
  [1] => gold-2
  [2] => blue night
  [3] => blue night-2
  [4] => silver morning 
  [5] => about earth
  [6] => about earth-2 
  [7] => about earth-3 
  [8] => sun vs king
 )
gold
blue night
silver morning
about earth
sun vs king
blue night-02
<?php
$title = ['gold','blue night','silver morning','about earth','sun vs king','blue night','about earth','gold','about earth'];

$value_counts = array_count_values($title);
#print_r($value_counts);
$expected_result = [];
foreach($value_counts as $k=>$v){
    if($v<2){
        $expected_result[] = $k;
    }else{
        $expected_result[] = $k;
        for($i=2;$i<=$v;$i++){
            $expected_result[] = $k."-".$i; 
        }
    }
}

print_r($expected_result);
Array ( 
  [0] => gold
  [1] => gold-2
  [2] => blue night
  [3] => blue night-2
  [4] => silver morning 
  [5] => about earth
  [6] => about earth-2 
  [7] => about earth-3 
  [8] => sun vs king
 )