douda5706 2016-06-26 14:43
浏览 22
已采纳

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

I've been successfully check same word on 2 different array, but my main problem if there is array contain same word of "picture" like on array number 3 -> 3, it just give me result 1. I want it display exact result like 2 because array number 3 -> 3 contain two "picture" word

Array 1

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

Array 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
        )

)

Result

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
)

Here is my code

$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条回答 默认 最新

  • duanbinian2243 2016-06-26 15:05
    关注

    Change the array order cause intersect return all array from the first array. Check the below code i have modified. It may helps. Thanks

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

    I have modified here

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

    Thanks,

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?