检查并计算每个阵列上的相同单词

时间:2016-06-26 14:43:22

标签: php arrays multidimensional-array

我已经成功检查了2个不同阵列上的相同单词,但是我的主要问题是,如果有数组包含相同的单词" picture"比如数组3 - > 3 ,它只给我结果1。 我希望它显示精确的结果,如2,因为数组3 - > 3 包含两个"图片"字

数组1

Array
(
    [0] => royalty
    [1] => free
    [2] => picture
)

数组2

Array
(
    [0] => Array
        (
            [0] => Affordable and search from millions of royalty free picture
        )

    [1] => Array
        (
            [0] => from millions of royalty picture
        )

    [2] => Array
        (
            [0] => Provides free picture upload and hosting
        )

    [3] => Array
        (
            [0] => Post your picture here Get permanent links picture
        )

    [4] => Array
        (
            [0] => Choose your own unique username to access image
        )

)

结果

Array 1
(
    [0] => 1
    [1] => 1
    [2] => 0
    [3] => 0
    [4] => 0
)
Array 2
(
    [0] => 1
    [1] => 0
    [2] => 1
    [3] => 0
    [4] => 0
)
Array 3
(
    [0] => 0
    [1] => 1
    [2] => 1
    [3] => 1
    [4] => 0
)

这是我的代码

$array1 = array('royalty', 'free', 'picture');

for ($i=0; $i < count($array1); $i++) { 
$array2 = array(
    array('Affordable and search from millions of royalty free'),
    array('from millions of royalty picture'),
    array('Provides free picture upload and hosting'),
    array('Post your picture here Get permanent links picture'),
    array('Choose your own unique username to access image')
);

foreach($array2 as &$item) {
    $item = count(array_intersect(explode(' ', $array1[$i]), explode(' ', $item[0])));
}

print_r($array2); }

3 个答案:

答案 0 :(得分:1)

更改数组顺序导致intersect从第一个数组返回所有数组。检查我修改过的以下代码。这可能有所帮助。感谢

   $array1 = array('royalty', 'free', 'picture');

   for ($i=0; $i < count($array1); $i++) { 
   $array2 = array(
   array('Affordable and search from millions of royalty free'),
   array('from millions of royalty picture'),
   array('Provides free picture upload and hosting'),
   array('Post your picture here Get permanent links picture picture'),
   array('Choose your own unique username to access image')
  );

  foreach($array2 as &$item) {
      $item = count(array_intersect(explode(' ', $item[0]), explode(' ',     $array1[$i])));
  }

print_r($array2);

我在这里修改了

**$item = count(array_intersect(explode(' ', $item[0]), explode(' ',     $array1[$i])));**

谢谢,

答案 1 :(得分:1)

试试这个:

$ array1 = array('royalty','free','picture');

for ($i=0; $i < count($array1); $i++) { 
    $array2 = array(
        array('Affordable and search from millions of royalty free'),
        array('from millions of royalty picture'),
        array('Provides free picture upload and hosting'),
        array('Post your picture here Get permanent links picture'),
        array('Choose your own unique username to access image')
    );

    foreach($array2 as &$item) {        
        $counts = array_count_values(explode(' ', $item[0]));
        $item = isset($counts[$array1[$i]]) ? $counts[$array1[$i]] : 0;
    }

    echo "<pre>";
    print_r($array2); 
}

答案 2 :(得分:1)

问题是您使用的是array_intersect,但参数的顺序错误。我个人认为array_filter可能更适合这里,主要是因为它提供了更好的可读代码。

我继续重写您的代码以使用array_filter方法。我们先来看看它:

$searchTerms = ['royalty', 'free', 'picture'];
$sentences = [
    'Affordable and search from millions of royalty free',
    'from millions of royalty picture',
    'Provides free picture upload and hosting',
    'Post your picture here Get permanent links picture',
    'Choose your own unique username to access image'
];
$result = [];
foreach ($searchTerms as $q) { 
    foreach($sentences as $sentence) {
        $result[$q][] = count(array_filter(explode(' ', $sentence), function($word) use ($q) {
            return $word == $q;
        } ));
    }
}

echo '<pre>';
print_r($result); 

输出将如下所示:

Array
(
    [royalty] => Array
        (
            [0] => 1
            [1] => 1
            [2] => 0
            [3] => 0
            [4] => 0
        )

    [free] => Array
        (
            [0] => 1
            [1] => 0
            [2] => 1
            [3] => 0
            [4] => 0
        )

    [picture] => Array
        (
            [0] => 0
            [1] => 1
            [2] => 1
            [3] => 2
            [4] => 0
        )

)

我希望你能原谅我在整个过程中做的一些无关的重构:

  • 声明循环外的所有变量。无需在每次迭代时重新声明它们,因为它们不会改变。
  • 为变量提供了一些更合理的名称
  • 稍微改变了“句子”的格式,不知道为什么他们在另一个数组里面。
  • 使输出成为一个关联数组,只是因为它更容易验证结果。

主要区别在于使用array_filter方法代替array_intersect。我认为这个功能不言自明,但可以随意询问是否有任何不清楚的地方。