Disregard both duplicate entries from multidimensional array

terrorfall

I am working with an API that brings back holiday data for employees. The data contains approved holiday requests, as well as cancelled holiday requests. The problem I am facing, is that cancelled holiday requests show up as duplicate entries in the following format:

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
            )

    )
)

Ideally the data would be formatted so that the cancelled property is utilized and I could filter off that (this has been requested of the developers). What I would like to do is remove both entries that have the same start_date and end_date.

Currently I am able to remove the duplicate, by using this function

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

        return $array;
    }

Couple of problems with this, in that I am still left with one of the entries, as the holiday request has been cancelled I don't want either of them in the data. Filtering the data to exclude all elements that have the action property of 'cancel' will still leave me with the original request as well.

The other problem is that the above function is only filtering based off start_date and not both start_date and end_date.

Had a look through the comments on this SO question, and in the comments on the PHP doc page for array_values and array_unique.

mhall

I have made some slight modifications to your current code, please see comments in the code:

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

Output:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to remove duplicate entries from multidimensional array?

From Dev

Removing duplicates entries from a multidimensional php array

From Dev

Remove duplicate entries from array of arrays (javascript)

From Dev

Remove duplicate values from a multidimensional array in PHP

From Dev

How to remove duplicate values from a multidimensional array?

From Dev

Remove duplicate combination of elements from multidimensional array

From Dev

Delete duplicate rows from Multidimensional Object array [,]?

From Dev

Remove duplicate values from a multidimensional array

From Dev

Merge and add duplicate integers from a multidimensional array

From Dev

remove duplicate values from multidimensional array

From Dev

Remove unnecessary value entries from multidimensional array in c?

From Dev

array issue with duplicate entries

From Dev

Duplicate entries for Array List

From Dev

Characters duplicate in a multidimensional array

From Dev

I have multidimensional array in php and want to remove duplicate entries on the basis of 3 column

From Dev

getting duplicate entries from array (instead of just removing them)

From Dev

Remove an element from array and save other duplicate entries

From Dev

JavaScript: How to remove the duplicate entries from array with respect to multiple keys

From Dev

Remove duplicate entries from string array column of postgres

From Dev

How to remove duplicate entries from associative array in php

From Dev

How to remove duplicate entries from array of arrays based on nested value?

From Dev

How to get rid of duplicate values from multidimensional array

From Dev

Remove item with duplicate ID from multidimensional array with criterion which to keep

From Dev

PHP- Remove duplicate value from multidimensional array

From Dev

remove duplicate values from multidimensional array by single value

From Dev

Remove duplicate from multidimensional array but keep lowest value

From Dev

Duplicate entries in the array created with PDO

From Dev

Printing an array list with duplicate entries

From Dev

Restrict duplicate entries into array JavaScript

Related Related

  1. 1

    How to remove duplicate entries from multidimensional array?

  2. 2

    Removing duplicates entries from a multidimensional php array

  3. 3

    Remove duplicate entries from array of arrays (javascript)

  4. 4

    Remove duplicate values from a multidimensional array in PHP

  5. 5

    How to remove duplicate values from a multidimensional array?

  6. 6

    Remove duplicate combination of elements from multidimensional array

  7. 7

    Delete duplicate rows from Multidimensional Object array [,]?

  8. 8

    Remove duplicate values from a multidimensional array

  9. 9

    Merge and add duplicate integers from a multidimensional array

  10. 10

    remove duplicate values from multidimensional array

  11. 11

    Remove unnecessary value entries from multidimensional array in c?

  12. 12

    array issue with duplicate entries

  13. 13

    Duplicate entries for Array List

  14. 14

    Characters duplicate in a multidimensional array

  15. 15

    I have multidimensional array in php and want to remove duplicate entries on the basis of 3 column

  16. 16

    getting duplicate entries from array (instead of just removing them)

  17. 17

    Remove an element from array and save other duplicate entries

  18. 18

    JavaScript: How to remove the duplicate entries from array with respect to multiple keys

  19. 19

    Remove duplicate entries from string array column of postgres

  20. 20

    How to remove duplicate entries from associative array in php

  21. 21

    How to remove duplicate entries from array of arrays based on nested value?

  22. 22

    How to get rid of duplicate values from multidimensional array

  23. 23

    Remove item with duplicate ID from multidimensional array with criterion which to keep

  24. 24

    PHP- Remove duplicate value from multidimensional array

  25. 25

    remove duplicate values from multidimensional array by single value

  26. 26

    Remove duplicate from multidimensional array but keep lowest value

  27. 27

    Duplicate entries in the array created with PDO

  28. 28

    Printing an array list with duplicate entries

  29. 29

    Restrict duplicate entries into array JavaScript

HotTag

Archive