数组检查Duplicacy和Unique值,并打印它们

时间:2011-07-08 06:31:50

标签: php arrays sorting

我们想知道,我们如何使用数组(关键字)检查数组(数据)。我们希望将结果存储到变量中以供进一步使用。 在这里我们详细解释你。

$array1 = array(John Wilkins, Poul Warner, Rodger Smith, David Bentham, David Wilkins, Brian Smith, David Warner)(Data)

$array2 = array(Wilkins, Warner, Smith, Bentham)(Keywords)

这里我们要检查array1中的array2。并将结果存储在变量中,并在以后或最后打印出来。

Checking Process will be....... 
Array2(Wilkins) checks all the items in Array1.
Array2(Warner) checks all the items in Array1.
Array2(Smith) checks all the items in Array1.
and so on... 

请帮助解决此问题..

由于 ROD

2 个答案:

答案 0 :(得分:0)

这样的事情怎么样:

$matches = array();

foreach ( $array2 as $value2 )
  foreach ( $array1 as $value1 )
    if ( stripos($value1, $value2) !== FALSE )
      $matches[] = $value1;

// Just in case a record matched more than once.
$matches = array_unique($matches);

答案 1 :(得分:0)

$array1 = array("John Wilkins", "Poul Warner", "Rodger Smith", "David Bentham", "David Wilkins", "Brian Smith", "David Warner");
$array2 = array("Wilkins", "Warner", "Smith", "Bentham");
$result = array();

foreach ( $array2 as $value ) {
    $result = array_merge( preg_grep("/$value/", $array1), $result );
}

$result = array_unique($result);

print_r($result);

输出:

Array
(
    [0] => David Bentham
    [1] => Rodger Smith
    [2] => Brian Smith
    [3] => Poul Warner
    [4] => David Warner
    [5] => John Wilkins
    [6] => David Wilkins
)