检查并计数每个数组中的相同单词



我已经成功地检查了相同的单词在2个不同的数组,但我的主要问题是,如果有数组包含相同的单词"图片"就像在数组数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); }

改变数组顺序,导致相交返回第一个数组的所有数组。检查下面我修改的代码。这可能会有帮助。由于

   $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])));**

谢谢,

试试这个:

$ array1 =阵列("皇室"、"免费"、"图片");

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); 
}

问题是您正在使用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法。我认为函数说明了一切,但如果有任何不清楚的地方,请随时询问。

相关内容

  • 没有找到相关文章

最新更新