从他的字符串中间比较数组位置

Compare array positions from the middle of his string

有谁知道如何比较所有数组元素但只从每个元素的中间开始比较?我会试着解释我自己。这是数组的转储,粗体 (**) 是要与每个数组元素进行比较的字符串部分:

array(3) {
[0]=> string(182) "<tr><td>Column1</td><td>Column2</td><td>Column3</td>**<td>Column4</td><td>Column5</td><td>Column6</td><td>Column7</td><td>Column8</td><td>Column9</td><td>Column10</td><td> </td></tr>**"

[1]=> string(182) "<tr><td>Column1</td><td>Column2</td><td>Column3</td>**<td>Column4</td><td>Column5</td><td>Column6</td><td>Column7</td><td>Column8</td><td>Column9</td><td>Column10</td><td> </td></tr>**"

[2]=> string(182) "<tr><td>Column1</td><td>Column2</td><td>Column3</td>**<td>Column4</td><td>Column5</td><td>Column6</td><td>Column7</td><td>Column8</td><td>Column9</td><td>Column10</td><td> </td></tr>**"
}

我不想比较整个字符串,只比较粗体部分以查看其数组元素之间是否匹配。如果有两个数组元素相同的部分,则必须删除一个。

你可以这样做:

$array = array(
    "<tr><td>Column1</td><td>Column2</td><td>Column3</td><td>Column4 Foo</td></tr>",
    "<tr><td>Column1</td><td>Column2</td><td>Column3</td><td>Column4 Bar</td></tr>",
    "<tr><td>Column1</td><td>Column2</td><td>Column3</td><td>Column4 Foo</td></tr>",
);

// Array of values (interesting part of) already seen
$alreadyInArray = array();

// Filter out duplicates
$array = array_filter(
    $array,
    function($val) use (&$alreadyInArray) {
        // Get everything "after the third occurrence of </td>"
        preg_match('#(?:.*?</td>){3}(.*)$#', $val, $matches);
        $partToCheck = $matches[1];

        // Have we seen this value before?
        if (isset($alreadyInArray[$partToCheck]))
            return false; // Duplicate - leave out of array

        // Not seen before, keep and add value to check array
        $alreadyInArray[$partToCheck] = true;
        return true;
    }
);
print_r($array);

输出:

Array
(
    [0] => <tr><td>Column1</td><td>Column2</td><td>Column3</td><td>Column4 Foo</td></tr>
    [1] => <tr><td>Column1</td><td>Column2</td><td>Column3</td><td>Column4 Bar</td></tr>
)