(PHP 5, PHP 7)
array_udiff — 데이터 비교 콜백함수를 사용하여 배열간의 차이를 계산
데이터 비교 콜백함수를 사용하여 배열간의 차이를 계산합니다. 데티터 비교에 내부함수를 사용하는 array_diff() 와는 다릅니다.
array1
첫번째 배열.
array2
두번째 배열.
value_compare_func
비교 콜백 함수.
이 비교 함수는 첫번째 인수가 두번째 인수보다 작거나, 같거나, 클 경우에 각각 0보다 작거나, 같거나, 큰 정수를 반환해야 합니다.
다른 배열 인자에 존재하지 않는 array1
의 모든값을 리턴합니다.
Example #1 stdClass 객체를 이용한 array_udiff() 예제
<?php
// 비교할 배열들
$array1 = array(new stdclass, new stdclass,
new stdclass, new stdclass,
);
$array2 = array(
new stdclass, new stdclass,
);
// 각각 오브젝트의 프로퍼티 마다 값 세팅.
$array1[0]->width = 11; $array1[0]->height = 3;
$array1[1]->width = 7; $array1[1]->height = 1;
$array1[2]->width = 2; $array1[2]->height = 9;
$array1[3]->width = 5; $array1[3]->height = 7;
$array2[0]->width = 7; $array2[0]->height = 5;
$array2[1]->width = 9; $array2[1]->height = 2;
function compare_by_area($a, $b) {
$areaA = $a->width * $a->height;
$areaB = $b->width * $b->height;
if ($areaA < $areaB) {
return -1;
} elseif ($areaA > $areaB) {
return 1;
} else {
return 0;
}
}
print_r(array_udiff($array1, $array2, 'compare_by_area'));
?>
위 예제의 출력:
Array ( [0] => stdClass Object ( [width] => 11 [height] => 3 ) [1] => stdClass Object ( [width] => 7 [height] => 1 ) )
Example #2 DateTime 객체를 이용한 array_udiff() 예제
<?php
class MyCalendar {
public $free = array();
public $booked = array();
public function __construct($week = 'now') {
$start = new DateTime($week);
$start->modify('Monday this week midnight');
$end = clone $start;
$end->modify('Friday this week midnight');
$interval = new DateInterval('P1D');
foreach (new DatePeriod($start, $interval, $end) as $freeTime) {
$this->free[] = $freeTime;
}
}
public function bookAppointment(DateTime $date, $note) {
$this->booked[] = array('date' => $date->modify('midnight'), 'note' => $note);
}
public function checkAvailability() {
return array_udiff($this->free, $this->booked, array($this, 'customCompare'));
}
public function customCompare($free, $booked) {
if (is_array($free)) $a = $free['date'];
else $a = $free;
if (is_array($booked)) $b = $booked['date'];
else $b = $booked;
if ($a == $b) {
return 0;
} elseif ($a > $b) {
return 1;
} else {
return -1;
}
}
}
// 주간 스케쥴 달력 생성
$myCalendar = new MyCalendar;
// 이주의 스케쥴 예약
$myCalendar->bookAppointment(new DateTime('Monday this week'), "Cleaning GoogleGuy's apartment.");
$myCalendar->bookAppointment(new DateTime('Wednesday this week'), "Going on a snowboarding trip.");
$myCalendar->bookAppointment(new DateTime('Friday this week'), "Fixing buggy code.");
// 예약일 대비 가용일 체크
echo "I'm available on the following days this week...\n\n";
foreach ($myCalendar->checkAvailability() as $free) {
echo $free->format('l'), "\n";
}
echo "\n\n";
echo "I'm busy on the following days this week...\n\n";
foreach ($myCalendar->booked as $booked) {
echo $booked['date']->format('l'), ": ", $booked['note'], "\n";
}
?>
위 예제의 출력:
I'm available on the following days this week... Tuesday Thursday I'm busy on the following days this week... Monday: Cleaning GoogleGuy's apartment. Wednesday: Going on a snowboarding trip. Friday: Fixing buggy code.
Note: 이 함수는 n차원 배열의 1차 배열만 체크하므로 주의하기 바랍니다. 물론 깊은 차원의 체크는 array_udiff($array1[0], $array2[0], "data_compare_func"); 로 가능합니다.