Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 忽略多维数组中的两个重复项_Php_Arrays_Multidimensional Array - Fatal编程技术网

Php 忽略多维数组中的两个重复项

Php 忽略多维数组中的两个重复项,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我正在使用一个API,它可以为员工带回假期数据。该数据包含已批准的假日请求以及已取消的假日请求。我面临的问题是,取消的假日请求以以下格式显示为重复条目: stdClass Object ( [leave_requests] => Array ( [0] => stdClass Object ( [employee] => stdClass Object

我正在使用一个API,它可以为员工带回假期数据。该数据包含已批准的假日请求以及已取消的假日请求。我面临的问题是,取消的假日请求以以下格式显示为重复条目:

stdClass Object
  (
     [leave_requests] => Array
     (
        [0] => stdClass Object
            (
                [employee] => stdClass Object
                    (
                        [id] => 30771
                    )

                [reviewed_by] => stdClass Object
                    (
                        [id] => 22734
                    )

                [reason] => 
                [type] => Holiday
                [deducted] => 1.0
                [cancelled] => 
                [id] => 626214
                [start_date] => 2015-08-20
                [half_start] => 
                [half_start_am_pm] => 
                [end_date] => 2015-08-20
                [half_end] => 
                [half_end_am_pm] => 
                [action] => request
                [status] => approved
                [notes] => 
                [created_at] => 2015-08-06T21:55:12+01:00
                [updated_at] => 2015-08-07T08:26:07+01:00
            )

        [1] => stdClass Object
            (
                [employee] => stdClass Object
                    (
                        [id] => 30771
                    )

                [reviewed_by] => stdClass Object
                    (
                        [id] => 22734
                    )

                [reason] => 
                [type] => Holiday
                [deducted] => 1.0
                [cancelled] => 
                [id] => 632745
                [start_date] => 2015-08-20
                [half_start] => 
                [half_start_am_pm] => 
                [end_date] => 2015-08-20
                [half_end] => 
                [half_end_am_pm] => 
                [action] => cancel
                [status] => approved
                [notes] => 
                [created_at] => 2015-08-12T17:50:32+01:00
                [updated_at] => 2015-08-12T17:53:46+01:00
            )

    )
)
理想情况下,数据将被格式化,以便使用取消的属性,并且我可以过滤掉它(这是开发人员要求的)。我想做的是删除具有相同开始日期和结束日期的两个条目

目前,我可以使用此功能删除副本

function super_unique($array)
    {
        $newArr = array();
        foreach ($array as $val) {
            $newArr[$val['startDate']] = $val;
        }
        $array = array_values($newArr);

        return $array;
    }
这有两个问题,因为我仍然有一个条目,因为假期请求已被取消,我不希望数据中有任何一个条目。过滤数据以排除所有具有action属性“cancel”的元素仍将保留原始请求

另一个问题是,上述函数仅基于开始日期进行过滤,而不是同时基于开始日期和结束日期进行过滤


查看了PHP文档页面上的注释,以及关于数组值和数组唯一性的注释。

我对您当前的代码做了一些轻微的修改,请参见代码中的注释:

function super_unique($array)
{
    $newArr = array();

    foreach ($array as $val) {
        // Create a key by combining start/end dates 
        $key = $val['startDate'].$val['endDate'];

        // Have we already seen this start/end date combination?
        if (array_key_exists($key, $newArr)) {
            $val = null; // Clear element value, like "remove this later on"
        }
        $newArr[$key] = $val; // Add to/update key index (actual value or null)
    }

    // Remove all elements with unset (null) values
    $array = array_filter(array_values($newArr));

    return $array;
}

$a = [['startDate' => '2015-08-20', // Exists twice - should be removed
       'endDate'   => '2015-08-22'],
      ['startDate' => '2015-08-20', // Unique start/end combination
       'endDate'   => '2015-08-20'],
      ['startDate' => '2015-08-21', // Unique start/end combination
       'endDate'   => '2015-08-21'],
      ['startDate' => '2015-08-20', // Exists twice - should be removed
       'endDate'   => '2015-08-22'],
      ['startDate' => '2015-08-22', // Unique start/end combination
       'endDate'   => '2015-08-20'],
];

print_r(super_unique($a));
输出:

Array
(
    [1] => Array
        (
            [startDate] => 2015-08-20
            [endDate] => 2015-08-20
        )

    [2] => Array
        (
            [startDate] => 2015-08-21
            [endDate] => 2015-08-21
        )

    [3] => Array
        (
            [startDate] => 2015-08-22
            [endDate] => 2015-08-20
        )
)