如何在不同的已定义键中比较相同值的多维数组

时间:2019-02-17 15:41:06

标签: php

我找不到以下任务的任何解决方案-请帮助: 我想获得多维数组的跟踪结果,如果DTEND == DTSTART,其中Summary-name是相同的(相似):

数组 (

[0] => Array
    (

        [DTEND] => Array

            (

                [value] => 20180219
                [type] => DATE

            )

        [DTSTART] => Array
            (
                [value] => 20190220
                [type] => DATE
            )

        [SUMMARY] => James Brown (AB123)
    )
[1] => Array
    (
        [DTEND] => Array
            (
                [value] => 20180708
                [type] => DATE
            )

        [DTSTART] => Array
            (
                [value] => 20180704
                [type] => DATE
            )

        [SUMMARY] => Johnny Cash (xy987)
    )
[2] => Array
    (
        [DTEND] => Array
            (
                [value] => 20180225
                [type] => DATE
            )

        [DTSTART] => Array
            (
                [value] => 20180219
                [type] => DATE
            )

        [SUMMARY] => Bob Marley (DE456)
    )
[3] => Array
    (
        [DTEND] => Array
            (
                [value] => 20181230
                [type] => DATE
            )

        [DTSTART] => Array
            (
                [value] => 20181229
                [type] => DATE
            )

        [SUMMARY] => Elvis Presley (FG789)
    )

[4] => Array
    (
        [DTEND] => Array
            (
                [value] => 20190112
                [type] => DATE
            )

        [DTSTART] => Array
            (
                [value] => 20181230
                [type] => DATE
            )

        [SUMMARY] => Elvis Presley
    )

  1. 匹配: 结束:[0] [DTEND] [值] = 20180219-[摘要] =詹姆斯·布朗(AB123)-不同,无法加入

开始:[2] [DTSTART] [值] = 20180219-[摘要] =>鲍勃·马利(DE456)-不同,无法加入

  1. 匹配: 结束:[3] [DTEND] [值] = 20181230-[摘要] =埃尔维斯·普雷斯利(FG789)-相同,可以加入

开始:[4] [DTSTART] [值] = 20181230-[摘要] =>埃尔维斯·普雷斯利-相同,可以加入

1 个答案:

答案 0 :(得分:0)

根据您的示例,这应该可行。

$matches = [];
$count = count($arr);
for ($i = 0; $i < $count; $i++) {
    for ($x = $i + 1; $x < $count; $x++) {

        // finds " (any uppercase letters or numbers)"
        $pattern = '/ \([A-Z0-9]+\)/';

        /// remove the numbers within parenthesis and compare summary values
        if (preg_replace($pattern, '', $arr[$i]['SUMMARY']) == preg_replace($pattern, '', $arr[$x]['SUMMARY'])) {

            if ($arr[$i]['DTEND'] === $arr[$x]['DTSTART'] || $arr[$x]['DTEND'] === $arr[$i]['DTSTART']) {
                $matches [] = [$arr[$i], $arr[$x]];
            }
        }

    }
}

var_dump($matches);