xref: /PHP-7.0/ext/standard/tests/array/007.phpt (revision db76b708)
1--TEST--
2Test array_diff and array_diff_assoc behaviour
3--FILE--
4<?php
5$a = array(1,"big"=>2,3,6,3,5,3,3,3,3,3,3,3,3,3,3);
6$b = array(2,2,3,3,3,3,3,3,3,3,3,3,3,3,3);
7$c = array(-1,1);
8echo '$a='.var_export($a,TRUE).";\n";
9echo '$b='.var_export($b,TRUE).";\n";
10echo '$c='.var_export($c,TRUE).";\n";
11var_dump(array_diff($a,$b,$c));
12var_dump(array_diff_assoc($a,$b,$c));
13$a = array(
14'a'=>2,
15'b'=>'some',
16'c'=>'done',
17'z'=>'foo',
18'f'=>5,
19'fan'=>'fen',
207=>18,
219=>25,
2211=>42,
2312=>42,
2445=>42,
2573=>'foo',
2695=>'some',
27'som3'=>'some',
28'want'=>'wanna');
29$b = array(
30'a'=>7,
317=>18,
329=>13,
3311=>42,
3445=>46,
35'som3'=>'some',
36'foo'=>'some',
37'goo'=>'foo',
38'f'=>5,
39'z'=>'equal'
40);
41$c = array(
4273=>'foo',
4395=>'some');
44echo '$a='.var_export($a,TRUE).";\n";
45echo '$b='.var_export($b,TRUE).";\n";
46echo '$c='.var_export($c,TRUE).";\n";
47echo "Results:\n\n";
48var_dump(array_diff($a,$b,$c));
49var_dump(array_diff_assoc($a,$b,$c));
50
51echo "-=-=-=-=-=-=-=-=- New functionality from 5.0.0 -=-=-=-=-=-=-=-\n";
52error_reporting(E_ALL);
53class cr {
54	private $priv_member;
55	public  $public_member;
56	function __construct($val) {
57		$this->priv_member = $val;
58		$this->public_member = $val;
59	}
60	static function comp_func_cr($a, $b) {
61		if ($a->priv_member === $b->priv_member) return 0;
62		return ($a->priv_member > $b->priv_member)? 1:-1;
63	}
64}
65
66function comp_func($a, $b) {
67	if ($a === $b) return 0;
68	return ($a > $b)? 1:-1;
69
70}
71
72function comp_func_cr($a, $b) {
73	if ($a->public_member === $b->public_member) return 0;
74	return ($a->public_member > $b->public_member)? 1:-1;
75}
76
77
78/*
79$a = array(1,"big"=>2,3,6,3,5,3,3,3,3,3,3,3,3,3,3);
80$b = array(2,2,3,3,3,3,3,3,3,3,3,3,3,3,3);
81$c = array(-1,1);
82echo '$a='.var_export($a,TRUE).";\n";
83echo '$b='.var_export($b,TRUE).";\n";
84echo '$c='.var_export($c,TRUE).";\n";
85var_dump(array_diff($a,$b,$c));
86var_dump(array_diff_assoc($a,$b,$c));
87var_dump(array_udiff($a, $b, $c, "comp_func"));
88var_dump(array_diff_uassoc($a,$b,$c, "comp_func"));
89*/
90
91/*
92 $a = array(new cr(9),new cr(12),new cr(23),new cr(4),new cr(-15),);
93 $b = array(new cr(9),new cr(22),new cr( 3),new cr(4),new cr(-15),);
94 var_dump(array_udiff($a, $b, "comp_func_cr"));
95*/
96$a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1=> new cr(4), 2 => new cr(-15),);
97$b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr( 3), 1=> new cr(4), 2 => new cr(-15),);
98
99echo '$a='.var_export($a,TRUE).";\n";
100echo '$b='.var_export($b,TRUE).";\n";
101echo 'var_dump(array_udiff_uassoc($a, $b, "comp_func_cr", "comp_func"));'."\n";
102var_dump(array_udiff_uassoc($a, $b, "comp_func_cr", "comp_func"));
103
104
105echo '$a='.var_export($a,TRUE).";\n";
106echo '$b='.var_export($b,TRUE).";\n";
107echo 'var_dump(array_udiff_uassoc($a, $b, array("cr", "comp_func_cr"), "comp_func"));'."\n";
108var_dump(array_udiff_uassoc($a, $b, array("cr", "comp_func_cr"), "comp_func"));
109
110
111echo '$a='.var_export($a,TRUE).";\n";
112echo '$b='.var_export($b,TRUE).";\n";
113echo 'var_dump(array_udiff($a, $b, "comp_func_cr"));'."\n";
114var_dump(array_udiff($a, $b, "comp_func_cr"));
115
116
117echo '$a='.var_export($a,TRUE).";\n";
118echo '$b='.var_export($b,TRUE).";\n";
119echo 'var_dump(array_udiff_assoc($a, $b, "comp_func_cr"));'."\n";
120var_dump(array_udiff_assoc($a, $b, "comp_func_cr"));
121
122?>
123--EXPECTF--
124$a=array (
125  0 => 1,
126  'big' => 2,
127  1 => 3,
128  2 => 6,
129  3 => 3,
130  4 => 5,
131  5 => 3,
132  6 => 3,
133  7 => 3,
134  8 => 3,
135  9 => 3,
136  10 => 3,
137  11 => 3,
138  12 => 3,
139  13 => 3,
140  14 => 3,
141);
142$b=array (
143  0 => 2,
144  1 => 2,
145  2 => 3,
146  3 => 3,
147  4 => 3,
148  5 => 3,
149  6 => 3,
150  7 => 3,
151  8 => 3,
152  9 => 3,
153  10 => 3,
154  11 => 3,
155  12 => 3,
156  13 => 3,
157  14 => 3,
158);
159$c=array (
160  0 => -1,
161  1 => 1,
162);
163array(2) {
164  [2]=>
165  int(6)
166  [4]=>
167  int(5)
168}
169array(5) {
170  [0]=>
171  int(1)
172  ["big"]=>
173  int(2)
174  [1]=>
175  int(3)
176  [2]=>
177  int(6)
178  [4]=>
179  int(5)
180}
181$a=array (
182  'a' => 2,
183  'b' => 'some',
184  'c' => 'done',
185  'z' => 'foo',
186  'f' => 5,
187  'fan' => 'fen',
188  7 => 18,
189  9 => 25,
190  11 => 42,
191  12 => 42,
192  45 => 42,
193  73 => 'foo',
194  95 => 'some',
195  'som3' => 'some',
196  'want' => 'wanna',
197);
198$b=array (
199  'a' => 7,
200  7 => 18,
201  9 => 13,
202  11 => 42,
203  45 => 46,
204  'som3' => 'some',
205  'foo' => 'some',
206  'goo' => 'foo',
207  'f' => 5,
208  'z' => 'equal',
209);
210$c=array (
211  73 => 'foo',
212  95 => 'some',
213);
214Results:
215
216array(5) {
217  ["a"]=>
218  int(2)
219  ["c"]=>
220  string(4) "done"
221  ["fan"]=>
222  string(3) "fen"
223  [9]=>
224  int(25)
225  ["want"]=>
226  string(5) "wanna"
227}
228array(9) {
229  ["a"]=>
230  int(2)
231  ["b"]=>
232  string(4) "some"
233  ["c"]=>
234  string(4) "done"
235  ["z"]=>
236  string(3) "foo"
237  ["fan"]=>
238  string(3) "fen"
239  [9]=>
240  int(25)
241  [12]=>
242  int(42)
243  [45]=>
244  int(42)
245  ["want"]=>
246  string(5) "wanna"
247}
248-=-=-=-=-=-=-=-=- New functionality from 5.0.0 -=-=-=-=-=-=-=-
249$a=array (
250  '0.1' =>
251  cr::__set_state(array(
252     'priv_member' => 9,
253     'public_member' => 9,
254  )),
255  '0.5' =>
256  cr::__set_state(array(
257     'priv_member' => 12,
258     'public_member' => 12,
259  )),
260  0 =>
261  cr::__set_state(array(
262     'priv_member' => 23,
263     'public_member' => 23,
264  )),
265  1 =>
266  cr::__set_state(array(
267     'priv_member' => 4,
268     'public_member' => 4,
269  )),
270  2 =>
271  cr::__set_state(array(
272     'priv_member' => -15,
273     'public_member' => -15,
274  )),
275);
276$b=array (
277  '0.2' =>
278  cr::__set_state(array(
279     'priv_member' => 9,
280     'public_member' => 9,
281  )),
282  '0.5' =>
283  cr::__set_state(array(
284     'priv_member' => 22,
285     'public_member' => 22,
286  )),
287  0 =>
288  cr::__set_state(array(
289     'priv_member' => 3,
290     'public_member' => 3,
291  )),
292  1 =>
293  cr::__set_state(array(
294     'priv_member' => 4,
295     'public_member' => 4,
296  )),
297  2 =>
298  cr::__set_state(array(
299     'priv_member' => -15,
300     'public_member' => -15,
301  )),
302);
303var_dump(array_udiff_uassoc($a, $b, "comp_func_cr", "comp_func"));
304array(3) {
305  ["0.1"]=>
306  object(cr)#%d (2) {
307    ["priv_member":"cr":private]=>
308    int(9)
309    ["public_member"]=>
310    int(9)
311  }
312  ["0.5"]=>
313  object(cr)#%d (2) {
314    ["priv_member":"cr":private]=>
315    int(12)
316    ["public_member"]=>
317    int(12)
318  }
319  [0]=>
320  object(cr)#%d (2) {
321    ["priv_member":"cr":private]=>
322    int(23)
323    ["public_member"]=>
324    int(23)
325  }
326}
327$a=array (
328  '0.1' =>
329  cr::__set_state(array(
330     'priv_member' => 9,
331     'public_member' => 9,
332  )),
333  '0.5' =>
334  cr::__set_state(array(
335     'priv_member' => 12,
336     'public_member' => 12,
337  )),
338  0 =>
339  cr::__set_state(array(
340     'priv_member' => 23,
341     'public_member' => 23,
342  )),
343  1 =>
344  cr::__set_state(array(
345     'priv_member' => 4,
346     'public_member' => 4,
347  )),
348  2 =>
349  cr::__set_state(array(
350     'priv_member' => -15,
351     'public_member' => -15,
352  )),
353);
354$b=array (
355  '0.2' =>
356  cr::__set_state(array(
357     'priv_member' => 9,
358     'public_member' => 9,
359  )),
360  '0.5' =>
361  cr::__set_state(array(
362     'priv_member' => 22,
363     'public_member' => 22,
364  )),
365  0 =>
366  cr::__set_state(array(
367     'priv_member' => 3,
368     'public_member' => 3,
369  )),
370  1 =>
371  cr::__set_state(array(
372     'priv_member' => 4,
373     'public_member' => 4,
374  )),
375  2 =>
376  cr::__set_state(array(
377     'priv_member' => -15,
378     'public_member' => -15,
379  )),
380);
381var_dump(array_udiff_uassoc($a, $b, array("cr", "comp_func_cr"), "comp_func"));
382array(3) {
383  ["0.1"]=>
384  object(cr)#%d (2) {
385    ["priv_member":"cr":private]=>
386    int(9)
387    ["public_member"]=>
388    int(9)
389  }
390  ["0.5"]=>
391  object(cr)#%d (2) {
392    ["priv_member":"cr":private]=>
393    int(12)
394    ["public_member"]=>
395    int(12)
396  }
397  [0]=>
398  object(cr)#%d (2) {
399    ["priv_member":"cr":private]=>
400    int(23)
401    ["public_member"]=>
402    int(23)
403  }
404}
405$a=array (
406  '0.1' =>
407  cr::__set_state(array(
408     'priv_member' => 9,
409     'public_member' => 9,
410  )),
411  '0.5' =>
412  cr::__set_state(array(
413     'priv_member' => 12,
414     'public_member' => 12,
415  )),
416  0 =>
417  cr::__set_state(array(
418     'priv_member' => 23,
419     'public_member' => 23,
420  )),
421  1 =>
422  cr::__set_state(array(
423     'priv_member' => 4,
424     'public_member' => 4,
425  )),
426  2 =>
427  cr::__set_state(array(
428     'priv_member' => -15,
429     'public_member' => -15,
430  )),
431);
432$b=array (
433  '0.2' =>
434  cr::__set_state(array(
435     'priv_member' => 9,
436     'public_member' => 9,
437  )),
438  '0.5' =>
439  cr::__set_state(array(
440     'priv_member' => 22,
441     'public_member' => 22,
442  )),
443  0 =>
444  cr::__set_state(array(
445     'priv_member' => 3,
446     'public_member' => 3,
447  )),
448  1 =>
449  cr::__set_state(array(
450     'priv_member' => 4,
451     'public_member' => 4,
452  )),
453  2 =>
454  cr::__set_state(array(
455     'priv_member' => -15,
456     'public_member' => -15,
457  )),
458);
459var_dump(array_udiff($a, $b, "comp_func_cr"));
460array(2) {
461  ["0.5"]=>
462  object(cr)#%d (2) {
463    ["priv_member":"cr":private]=>
464    int(12)
465    ["public_member"]=>
466    int(12)
467  }
468  [0]=>
469  object(cr)#%d (2) {
470    ["priv_member":"cr":private]=>
471    int(23)
472    ["public_member"]=>
473    int(23)
474  }
475}
476$a=array (
477  '0.1' =>
478  cr::__set_state(array(
479     'priv_member' => 9,
480     'public_member' => 9,
481  )),
482  '0.5' =>
483  cr::__set_state(array(
484     'priv_member' => 12,
485     'public_member' => 12,
486  )),
487  0 =>
488  cr::__set_state(array(
489     'priv_member' => 23,
490     'public_member' => 23,
491  )),
492  1 =>
493  cr::__set_state(array(
494     'priv_member' => 4,
495     'public_member' => 4,
496  )),
497  2 =>
498  cr::__set_state(array(
499     'priv_member' => -15,
500     'public_member' => -15,
501  )),
502);
503$b=array (
504  '0.2' =>
505  cr::__set_state(array(
506     'priv_member' => 9,
507     'public_member' => 9,
508  )),
509  '0.5' =>
510  cr::__set_state(array(
511     'priv_member' => 22,
512     'public_member' => 22,
513  )),
514  0 =>
515  cr::__set_state(array(
516     'priv_member' => 3,
517     'public_member' => 3,
518  )),
519  1 =>
520  cr::__set_state(array(
521     'priv_member' => 4,
522     'public_member' => 4,
523  )),
524  2 =>
525  cr::__set_state(array(
526     'priv_member' => -15,
527     'public_member' => -15,
528  )),
529);
530var_dump(array_udiff_assoc($a, $b, "comp_func_cr"));
531array(3) {
532  ["0.1"]=>
533  object(cr)#%d (2) {
534    ["priv_member":"cr":private]=>
535    int(9)
536    ["public_member"]=>
537    int(9)
538  }
539  ["0.5"]=>
540  object(cr)#%d (2) {
541    ["priv_member":"cr":private]=>
542    int(12)
543    ["public_member"]=>
544    int(12)
545  }
546  [0]=>
547  object(cr)#%d (2) {
548    ["priv_member":"cr":private]=>
549    int(23)
550    ["public_member"]=>
551    int(23)
552  }
553}
554