PHP比较两个数组,过滤器数组具有特定值

时间:2019-12-17 02:45:00

标签: php

有两个数组。

files数组保存文件名。

contents数组保存html文本。

我要检查内容中未包含的过滤器文件。

$files = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg"];
$contents = ["<p>random text</p>", "<p>another random text</p>", "<img src='1.jpg'>"];

我尝试如下所示,但效果不佳。

$filter = []; 
foreach ($contents as $key => $content) {

    foreach($files as $key => $file) {

        if(strpos($content, $file) == false) {
            $filter[$key] = $file;
        }               
    }
}

非常感谢您。

3 个答案:

答案 0 :(得分:2)

看我的代码:

$files = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg"];
$contents = ["<p>random text</p>", "<p>another random text</p>", "<img src='1.jpg'>"];

$included = [];
foreach ($contents as $content) {
    foreach ($files as $file) {
        if (strpos(strtolower($content), strtolower($file))) {
            $included[] = $file;
        }
    }
}

print_r(array_diff($files, $included));
Array ( [1] => 2.jpg [2] => 3.jpg [3] => 4.jpg [4] => 5.jpg )

答案 1 :(得分:1)

您可以尝试以下方法:

foreach ($contents as $content) {

    foreach($files as $file) {

        if(!strpos($content, $file)) {
            if (!in_array($file, $filter)) {
                $filter[] = $file;
            }
        } else {
            if (($key = array_search($file, $filter)) !== false) {
                unset($filter[$key]);
            }
        }
    }
}

答案 2 :(得分:0)

尝试一下:

import pandas as pd
import matplotlib.pyplot as plt 
df = pd.DataFrame({'pAgent' :[22596,22596,44456,6655,22596,22596,42244,22596,22596,22596] ,'day':  [1,2,3,4,5,6,7,8,9,10]  })
fig, axes = plt.subplots(figsize=(10, 8))
axes.scatter(df[df['pAgent'] == 22596]['day'], df[df['pAgent'] == 22596]['pAgent'], marker = '|',s=10**3)
axes.set(xlabel='days')
fig.show()

结果: 数组([1] => 2.jpg [2] => 3.jpg [3] => 4.jpg [4] => 5.jpg)