reference/array/functions/array-udiff.xml
56509d07ae636f076057f55bbb2572ab7b7a39eb
...
...
@@ -9,10 +9,9 @@
9
9
&reftitle.description;
10
10
<methodsynopsis>
11
11
<type>array</type><methodname>array_udiff</methodname>
12
-
<methodparam><type>array</type><parameter>array1</parameter></methodparam>
13
-
<methodparam><type>array</type><parameter>array2</parameter></methodparam>
14
-
<methodparam choice="opt"><type>array</type><parameter> ...</parameter></methodparam>
15
-
<methodparam><type>callback</type><parameter>data_compare_func</parameter></methodparam>
12
+
<methodparam><type>array</type><parameter>array</parameter></methodparam>
13
+
<methodparam rep="repeat"><type>array</type><parameter>arrays</parameter></methodparam>
14
+
<methodparam><type>callable</type><parameter>value_compare_func</parameter></methodparam>
16
15
</methodsynopsis>
17
16
<para>
18
17
Computes the difference of arrays by using a callback function for data
...
...
@@ -25,7 +24,7 @@
25
24
<para>
26
25
<variablelist>
27
26
<varlistentry>
28
-
<term><parameter>array1</parameter></term>
27
+
<term><parameter>array</parameter></term>
29
28
<listitem>
30
29
<para>
31
30
The first array.
...
...
@@ -33,25 +32,18 @@
33
32
</listitem>
34
33
</varlistentry>
35
34
<varlistentry>
36
-
<term><parameter>array2</parameter></term>
35
+
<term><parameter>arrays</parameter></term>
37
36
<listitem>
38
37
<para>
39
-
The second array.
38
+
Arrays to compare against.
40
39
</para>
41
40
</listitem>
42
41
</varlistentry>
43
42
<varlistentry>
44
-
<term><parameter>data_compare_func</parameter></term>
43
+
<term><parameter>value_compare_func</parameter></term>
45
44
<listitem>
46
-
<para>
47
-
The callback comparison function.
48
-
</para>
49
-
<para>
50
-
The user supplied callback function is used for comparison.
51
-
It must return an integer less than, equal to, or greater than zero if
52
-
the first argument is considered to be respectively less than, equal
53
-
to, or greater than the second.
54
-
</para>
45
+
&sort.callback.description;
46
+
&sort.callback.description.presort;
55
47
</listitem>
56
48
</varlistentry>
57
49
</variablelist>
...
...
@@ -60,7 +52,7 @@
60
52
<refsect1 role="returnvalues">
61
53
&reftitle.returnvalues;
62
54
<para>
63
-
Returns an array containing all the values of <parameter>array1</parameter>
55
+
Returns an array containing all the values of <parameter>array</parameter>
64
56
that are not present in any of the other arguments.
65
57
</para>
66
58
</refsect1>
...
...
@@ -68,28 +60,42 @@
68
60
&reftitle.examples;
69
61
<para>
70
62
<example>
71
-
<title><function>array_udiff</function> example</title>
63
+
<title><function>array_udiff</function> example using stdClass Objects</title>
72
64
<programlisting role="php">
73
65
<![CDATA[
74
66
<?php
75
-
class cr {
76
-
private $priv_member;
77
-
function cr($val)
78
-
{
79
-
$this->priv_member = $val;
80
-
}
67
+
// Arrays to compare
68
+
$array1 = array(new stdClass, new stdClass,
69
+
new stdClass, new stdClass,
70
+
);
71
+

72
+
$array2 = array(
73
+
new stdClass, new stdClass,
74
+
);
81
75

82
-
static function comp_func_cr($a, $b)
83
-
{
84
-
if ($a->priv_member === $b->priv_member) return 0;
85
-
return ($a->priv_member > $b->priv_member)? 1:-1;
76
+
// Set some properties for each object
77
+
$array1[0]->width = 11; $array1[0]->height = 3;
78
+
$array1[1]->width = 7; $array1[1]->height = 1;
79
+
$array1[2]->width = 2; $array1[2]->height = 9;
80
+
$array1[3]->width = 5; $array1[3]->height = 7;
81
+

82
+
$array2[0]->width = 7; $array2[0]->height = 5;
83
+
$array2[1]->width = 9; $array2[1]->height = 2;
84
+

85
+
function compare_by_area($a, $b) {
86
+
$areaA = $a->width * $a->height;
87
+
$areaB = $b->width * $b->height;
88
+
89
+
if ($areaA < $areaB) {
90
+
return -1;
91
+
} elseif ($areaA > $areaB) {
92
+
return 1;
93
+
} else {
94
+
return 0;
86
95
}
87
96
}
88
-
$a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1=> new cr(4), 2 => new cr(-15),);
89
-
$b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1=> new cr(4), 2 => new cr(-15),);
90
97

91
-
$result = array_udiff($a, $b, array("cr", "comp_func_cr"));
92
-
print_r($result);
98
+
print_r(array_udiff($array1, $array2, 'compare_by_area'));
93
99
?>
94
100
]]>
95
101
</programlisting>
...
...
@@ -98,14 +104,16 @@ print_r($result);
98
104
<![CDATA[
99
105
Array
100
106
(
101
-
[0.5] => cr Object
107
+
[0] => stdClass Object
102
108
(
103
-
[priv_member:private] => 12
109
+
[width] => 11
110
+
[height] => 3
104
111
)
105
112

106
-
[0] => cr Object
113
+
[1] => stdClass Object
107
114
(
108
-
[priv_member:private] => 23
115
+
[width] => 7
116
+
[height] => 1
109
117
)
110
118

111
119
)
...
...
@@ -113,6 +121,89 @@ Array
113
121
</screen>
114
122
</example>
115
123
</para>
124
+
<para>
125
+
<example>
126
+
<title><function>array_udiff</function> example using DateTime Objects</title>
127
+
<programlisting role="php">
128
+
<![CDATA[
129
+
<?php
130
+
class MyCalendar {
131
+
public $free = array();
132
+
public $booked = array();
133
+

134
+
public function __construct($week = 'now') {
135
+
$start = new DateTime($week);
136
+
$start->modify('Monday this week midnight');
137
+
$end = clone $start;
138
+
$end->modify('Friday this week midnight');
139
+
$interval = new DateInterval('P1D');
140
+
foreach (new DatePeriod($start, $interval, $end) as $freeTime) {
141
+
$this->free[] = $freeTime;
142
+
}
143
+
}
144
+

145
+
public function bookAppointment(DateTime $date, $note) {
146
+
$this->booked[] = array('date' => $date->modify('midnight'), 'note' => $note);
147
+
}
148
+

149
+
public function checkAvailability() {
150
+
return array_udiff($this->free, $this->booked, array($this, 'customCompare'));
151
+
}
152
+
153
+
public function customCompare($free, $booked) {
154
+
if (is_array($free)) $a = $free['date'];
155
+
else $a = $free;
156
+
if (is_array($booked)) $b = $booked['date'];
157
+
else $b = $booked;
158
+
if ($a == $b) {
159
+
return 0;
160
+
} elseif ($a > $b) {
161
+
return 1;
162
+
} else {
163
+
return -1;
164
+
}
165
+
}
166
+
}
167
+

168
+
// Create a calendar for weekly appointments
169
+
$myCalendar = new MyCalendar;
170
+

171
+
// Book some appointments for this week
172
+
$myCalendar->bookAppointment(new DateTime('Monday this week'), "Cleaning GoogleGuy's apartment.");
173
+
$myCalendar->bookAppointment(new DateTime('Wednesday this week'), "Going on a snowboarding trip.");
174
+
$myCalendar->bookAppointment(new DateTime('Friday this week'), "Fixing buggy code.");
175
+

176
+
// Check availability of days by comparing $booked dates against $free dates
177
+
echo "I'm available on the following days this week...\n\n";
178
+
foreach ($myCalendar->checkAvailability() as $free) {
179
+
echo $free->format('l'), "\n";
180
+
}
181
+
echo "\n\n";
182
+
echo "I'm busy on the following days this week...\n\n";
183
+
foreach ($myCalendar->booked as $booked) {
184
+
echo $booked['date']->format('l'), ": ", $booked['note'], "\n";
185
+
}
186
+
?>
187
+
]]>
188
+
</programlisting>
189
+
&example.outputs;
190
+
<screen>
191
+
<![CDATA[
192
+
I'm available on the following days this week...
193
+

194
+
Tuesday
195
+
Thursday
196
+

197
+

198
+
I'm busy on the following days this week...
199
+

200
+
Monday: Cleaning GoogleGuy's apartment.
201
+
Wednesday: Going on a snowboarding trip.
202
+
Friday: Fixing buggy code.
203
+
]]>
204
+
</screen>
205
+
</example>
206
+
</para>
116
207
</refsect1>
117
208
<refsect1 role="notes">
118
209
&reftitle.notes;
119
210