Php 如何仅从preg_match输出中删除真实副本?
是的,我知道Php 如何仅从preg_match输出中删除真实副本?,php,arrays,duplicates,preg-match,Php,Arrays,Duplicates,Preg Match,是的,我知道array\u unique函数,但问题是match可能在我的搜索词中有合法的重复项,例如: $str = "fruit1: banana, fruit2: orange, fruit3: banana, fruit4: apple, fruit5: banana"; preg_match("@fruit1: (?<fruit1>\w+), fruit2: orange, fruit3: (banana), fruit4: (?<fruit4>apple),
array\u unique
函数,但问题是match可能在我的搜索词中有合法的重复项,例如:
$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);
因此,真正重复的键只有[0]和[2],但array\u unique
给出:
Array
(
[fruit1] => banana
[fruit4] => apple
)
我自己找到的,解决方案是一个while循环,删除后续的键,但它不是数字键:
while (next($match) !== false) {
if (!is_int(key($match))) {
next($match);
unset($m[key($match)]);
}
}
reset($match);
我自己找到的,解决方案是一个while循环,删除后续的键,但它不是数字键:
while (next($match) !== false) {
if (!is_int(key($match))) {
next($match);
unset($m[key($match)]);
}
}
reset($match);
这是我对您问题的解决方案:
unset($matches[0]);
$matches = array_unique($matches);
这是我对您问题的解决方案:
unset($matches[0]);
$matches = array_unique($matches);
为什么只有[0]和[2]重复?因为如果正则表达式有一个命名范围,它会生成两个键,一个命名键和一个整数,但未命名范围只提供整数键。一个命名的范围和另一个未命名的范围可以有相同的值纯粹是偶然的。所以使用array_unique是不安全的。为什么只有
0
和1
是重复的,这真的没有意义。。关于3
?[3]来自未命名的范围,此副本来自字符串,我的意思是仅删除preg\u match生成的其他副本是否需要使用preg\u match?您可以使用explode string并使用foreach创建新数组。。然后使用array_unique。为什么只有[0]和[2]个重复项?因为如果正则表达式有一个命名范围,它会生成两个键,一个命名键和一个整数,但未命名范围只提供整数键。一个命名的范围和另一个未命名的范围可以有相同的值纯粹是偶然的。所以使用array_unique是不安全的。为什么只有0
和1
是重复的,这真的没有意义。。关于3
?[3]来自未命名的范围,此副本来自字符串,我的意思是仅删除preg\u match生成的其他副本是否需要使用preg\u match?您可以使用explode string并使用foreach创建新数组。。然后使用数组_unique。