【问题标题】:Compare string values and return common values (avoid repetition)比较字符串值并返回常用值(避免重复)
【发布时间】:2014-05-07 07:12:46
【问题描述】:

我有三个字符串,我需要比较每个字符串,并且必须通过排除重复值来返回唯一值。

$str1 = 1,2,4 ;
$str2 = 2,6 ;
$str3 = 1,4,6 ;

比较这个字符串,返回共同值,避免重复值。

输出为:

$output = 1,2,4,6

避免重复。

【问题讨论】:

  • 你的函数在哪里?
  • 你在比较什么?需要做什么?
  • 我有三个像这样的输入 str1 , str2 , str3 .. 现在我需要做的是像这样比较和返回值。我不知道如何在 php 中进行比较

标签: php arrays regex function output


【解决方案1】:

说明:

连接三个字符串并使用逗号运算符将它们分解为一个数组,使用array_unique 从该数组中找到唯一的条目,然后最终将其分解。

echo implode(',',array_unique(explode(',',$str1.','.$str2.','.$str3)));

Demonstration - PHP Native Functions

正则表达式 - 解决方案

$str = preg_match_all('@[0-9]@',$str1.$str2.$str3,$mtch);
echo implode(',',array_unique($mtch[0]));

Demonstration - Regular Expressions

【讨论】:

  • 哈哈 nandri maams :) 很高兴认识一个泰米尔人 :D
猜你喜欢
  • 1970-01-01
  • 2013-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多