Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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_Arrays_Regex - Fatal编程技术网

PHP-检查数组中的重复项并更新循环中的重复值

PHP-检查数组中的重复项并更新循环中的重复值,php,arrays,regex,Php,Arrays,Regex,给出: $mymarker = '/MARKER[0-9]{2}/'; preg_match_all($mymarker, $mycontent, $matches); var_dump($matches); 如我们所见,MARKER13出现两次。此重复实例可能与MARKER05、MARKER07和MARKER13一起出现 第二个值需要更新为MARKERXX+1,以便 第二次出现: Marker05必须更新为MARKER06 Marker07必须更新为MARKER08 MARKER13必须更新

给出:

$mymarker = '/MARKER[0-9]{2}/';
preg_match_all($mymarker, $mycontent, $matches);
var_dump($matches);
如我们所见,MARKER13出现两次。此重复实例可能与MARKER05、MARKER07和MARKER13一起出现

第二个值需要更新为MARKERXX+1,以便 第二次出现:

Marker05必须更新为MARKER06 Marker07必须更新为MARKER08 MARKER13必须更新为MARKER14

我们如何在循环中设置它,以检查DUP并将重复值更新为下一个值


谢谢

可能有更简单的方法,但你可以想出以下方法

array(1) {
  [0]=>
  array(18) {
    [0]=>
    string(8) "MARKER00"
    [1]=>
    string(8) "MARKER01"
    [2]=>
    string(8) "MARKER02"
    [3]=>
    string(8) "MARKER04"
    [4]=>
    string(8) "MARKER05"
    [5]=>
    string(8) "MARKER07"
    [6]=>
    string(8) "MARKER09"
    [7]=>
    string(8) "MARKER13"
    [8]=>
    string(8) "MARKER13"
    [9]=>
    string(8) "MARKER16"
    [10]=>
    string(8) "MARKER15"
    [11]=>
    string(8) "MARKER21"
    [12]=>
    string(8) "MARKER31"
    [13]=>
    string(8) "MARKER22"
    [14]=>
    string(8) "MARKER24"
    [15]=>
    string(8) "MARKER26"
    [16]=>
    string(8) "MARKER80"
    [17]=>
    string(8) "MARKER81"
  }
}

谢谢。你的回答给了我一个避免循环部分的想法。MARKERXX是html文档中的位置,我在MARKERXX和下一个MARKERXX上按顺序拆分(XX可以是任意数字,不一定按顺序排列),以映射到db列。标记xx通过预更换获得。因此,由于只有固定(3)个标记可以在整个文件集中重复,我运行了两次preg_replace($patterns,$replacements,1),一次运行05、07、13,下一次运行06、08和14,并在数组中得到了非重复标记xx。:)
<?php
$matches = array("MARKER00","MARKER01","MARKER02","MARKER04","MARKER05","MARKER07","MARKER09","MARKER13","MARKER13","MARKER16","MARKER15","MARKER21","MARKER31","MARKER22","MARKER24","MARKER26","MARKER80","MARKER81");

$duplicates = array("MARKER05", "MARKER07", "MARKER13");
$len = count($matches);

for ($i=0;$i<$len;$i++) {
    $marker = $matches[$i];

    if (in_array($marker, $duplicates)) {
        // one of our markers
        // check if the next is the same
        // if so update it
        if ($i<$len-1) {
            if ($marker == $matches[$i+1]) {
                # split the string into a letter and a digit part
                # with lookarounds (behind/ahead, both positive)
                list($text,$number) = preg_split('~(?<=[A-Z])(?=\d)~', $matches[$i+1]);
                $number = intval($number);
                # increase the intval'd number by one
                # and apply it to the original array
                $number += 1;
                $matches[$i+1] = $text . $number;
            }
        }
    }
}

print_r($matches);
// updated to MARKER14

?>