TryHardz
TryHardz

Reputation: 159

How can I remove duplicate array in between two arrays

I want to remove same array between 2 array

1 )  $getAlldate

array:8 [▼
  0 => "2018-08-17"
  1 => "2018-08-20"
  2 => "2018-08-21"
  3 => "2018-08-22"
  4 => "2018-08-23"
  5 => "2018-08-24"
  6 => "2018-08-27"
  7 => "2018-08-28"
]

2) $getHoliday
array:7 [▼
  0 => "2019-1-1"
  1 => "2019-3-6"
  2 => "2019-2-28"
  3 => "2019-5-2"
  4 => "2019-4-25"
  5 => "2018-8-27"
  6 => "2018-8-28"
]

As you see in my array 2018-08-27,2018-08-28 are duplicate with $getholiday I try to use array_diff and It still didnt work

$a = array_diff($getAllDate, $getHoliday);
$b = array_unique($a);

How can I output like this $getalldate remove same value $getHoliday

array:8 [▼
  0 => "2018-08-17"
  1 => "2018-08-20"
  2 => "2018-08-21"
  3 => "2018-08-22"
  4 => "2018-08-23"
  5 => "2018-08-24"
]

Upvotes: 1

Views: 67

Answers (1)

Ketan Yekale
Ketan Yekale

Reputation: 2213

The format of both the dates are diffrent.

Try:

PHP Code:

$getAlldate =  [
   "2018-08-17",
   "2018-08-20",
   "2018-08-21",
   "2018-08-22",
   "2018-08-23",
   "2018-08-24",
   "2018-08-27",
   "2018-08-28"
];


$getHoliday=[
   "2019-1-1",
   "2019-3-6",
   "2019-2-28",
   "2019-5-2",
   "2019-4-25",
   "2018-8-27",
   "2018-8-28"
];

$formattedHoliday = array_map(function ($holiday) {
    $alldateFormat = 'Y-m-d';
    $HolidayFormat = 'Y-n-d';
    $date = DateTime::createFromFormat($HolidayFormat, $holiday);
    return $date->format($alldateFormat);
}, $getHoliday);

var_dump(array_diff($getAlldate, $formattedHoliday));

Output: https://3v4l.org/rCLCk

Refer:

DateTime::createFromFormat: http://php.net/manual/en/datetime.createfromformat.php

DateTime::format: http://php.net/manual/en/datetime.format.php

Upvotes: 2

Related Questions