本文介绍了如何从preg_match输出中只删除真正的重复项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是的,我知道 array_unique
函数,但事实是该匹配在我的搜索字词中可能有合法的重复项,例如:
Yes I know array_unique
function, but the thing is that match might have a legitimate duplicates in my search term for example:
$str = "fruit1: banana, fruit2: orange, fruit3: banana, fruit4: apple, fruit5: banana";
preg_match("@fruit1: (?<fruit1>\w+), fruit2: orange, fruit3: (banana), fruit4: (?<fruit4>apple), fruit5: (banana)@",$str,$match);
array_shift($match); // I dont need whole match
print_r($match);
输出是:
Array
(
[fruit1] => banana
[0] => banana
[1] => banana
[fruit4] => apple
[2] => apple
[3] => banana
)
所以真正重复的唯一键是[0]和[2],但是 array_unique
给出:
So the only keys that are real duplicates are [0] and [2] but array_unique
gives:
Array
(
[fruit1] => banana
[fruit4] => apple
)
推荐答案
我发现自己,解决方案是一段时间删除后续密钥的循环是不是数值的:
I found it myself, solution is a while loop that deletes subsequent key is one that it is at is not numerical:
while (next($match) !== false) {
if (!is_int(key($match))) {
next($match);
unset($m[key($match)]);
}
}
reset($match);
这篇关于如何从preg_match输出中只删除真正的重复项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!