xref: /PHP-8.1/ext/standard/tests/array/008.phpt (revision 31e1cd13)
1--TEST--
2Test array_intersect and array_intersect_assoc behaviour
3--FILE--
4<?php
5//-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=- TEST 1 -=-=-=-=-
6$a = array(1,"big"=>2,2,6,3,5,3,3,454,'some_string',3,3,3,3,3,3,3,3,17);
7$b = array(2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,17,25,'some_string',7,8,9,109,78,17);
8$c = array(-1,2,1,15,25,17);
9echo str_repeat("-=",10)." TEST 1 ".str_repeat("-=",20)."\n";
10echo '$a='.var_export($a,TRUE).";\n";
11echo '$b='.var_export($b,TRUE).";\n";
12echo '$c='.var_export($c,TRUE).";\n";
13
14echo 'array_intersect($a,$b,$c);'."\n";
15var_dump(array_intersect($a,$b,$c));
16
17echo 'array_intersect_assoc($a,$b,$c);'."\n";
18var_dump(array_intersect_assoc($a,$b,$c));
19
20echo 'array_intersect($a,$b);'."\n";
21var_dump(array_intersect($a,$b));
22
23echo 'array_intersect_assoc($a,$b);'."\n";
24var_dump(array_intersect_assoc($a,$b));
25
26//-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=- TEST 2 -=-=-=-=-=-
27$a = array(
28'a'=>2,
29'b'=>'some',
30'c'=>'done',
31'z'=>'foo',
32'f'=>5,
33'fan'=>'fen',
34'bad'=>'bed',
35'gate'=>'web',
367=>18,
379=>25,
3811=>42,
3912=>42,
4045=>42,
4173=>'foo',
4295=>'some',
43'som3'=>'some',
44'want'=>'wanna');
45
46
47$b = array(
48'a'=>7,
497=>18,
509=>13,
5111=>42,
5245=>46,
53'som3'=>'some',
54'foo'=>'some',
55'goo'=>'foo',
56'f'=>5,
57'z'=>'equal',
58'gate'=>'web'
59);
60$c = array(
61'gate'=>'web',
6273=>'foo',
6395=>'some'
64);
65
66echo str_repeat("-=",10)." TEST 2 ".str_repeat("-=",20)."\n";
67echo '$a='.var_export($a,TRUE).";\n";
68echo '$b='.var_export($b,TRUE).";\n";
69echo '$c='.var_export($c,TRUE).";\n";
70echo "\n\nResults:\n\n";
71
72echo 'array_intersect($a,$b,$c);'."\n";
73var_dump(array_intersect($a,$b,$c));
74
75echo 'array_intersect_assoc($a,$b,$c);'."\n";
76var_dump(array_intersect_assoc($a,$b,$c));
77
78echo 'array_intersect($a,$b);'."\n";
79var_dump(array_intersect($a,$b));
80
81echo 'array_intersect_assoc($a,$b);'."\n";
82var_dump(array_intersect_assoc($a,$b));
83?>
84--EXPECT--
85-=-=-=-=-=-=-=-=-=-= TEST 1 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
86$a=array (
87  0 => 1,
88  'big' => 2,
89  1 => 2,
90  2 => 6,
91  3 => 3,
92  4 => 5,
93  5 => 3,
94  6 => 3,
95  7 => 454,
96  8 => 'some_string',
97  9 => 3,
98  10 => 3,
99  11 => 3,
100  12 => 3,
101  13 => 3,
102  14 => 3,
103  15 => 3,
104  16 => 3,
105  17 => 17,
106);
107$b=array (
108  0 => 2,
109  1 => 2,
110  2 => 3,
111  3 => 3,
112  4 => 3,
113  5 => 3,
114  6 => 3,
115  7 => 3,
116  8 => 3,
117  9 => 3,
118  10 => 3,
119  11 => 3,
120  12 => 3,
121  13 => 3,
122  14 => 3,
123  15 => 17,
124  16 => 25,
125  17 => 'some_string',
126  18 => 7,
127  19 => 8,
128  20 => 9,
129  21 => 109,
130  22 => 78,
131  23 => 17,
132);
133$c=array (
134  0 => -1,
135  1 => 2,
136  2 => 1,
137  3 => 15,
138  4 => 25,
139  5 => 17,
140);
141array_intersect($a,$b,$c);
142array(3) {
143  ["big"]=>
144  int(2)
145  [1]=>
146  int(2)
147  [17]=>
148  int(17)
149}
150array_intersect_assoc($a,$b,$c);
151array(1) {
152  [1]=>
153  int(2)
154}
155array_intersect($a,$b);
156array(15) {
157  ["big"]=>
158  int(2)
159  [1]=>
160  int(2)
161  [3]=>
162  int(3)
163  [5]=>
164  int(3)
165  [6]=>
166  int(3)
167  [8]=>
168  string(11) "some_string"
169  [9]=>
170  int(3)
171  [10]=>
172  int(3)
173  [11]=>
174  int(3)
175  [12]=>
176  int(3)
177  [13]=>
178  int(3)
179  [14]=>
180  int(3)
181  [15]=>
182  int(3)
183  [16]=>
184  int(3)
185  [17]=>
186  int(17)
187}
188array_intersect_assoc($a,$b);
189array(10) {
190  [1]=>
191  int(2)
192  [3]=>
193  int(3)
194  [5]=>
195  int(3)
196  [6]=>
197  int(3)
198  [9]=>
199  int(3)
200  [10]=>
201  int(3)
202  [11]=>
203  int(3)
204  [12]=>
205  int(3)
206  [13]=>
207  int(3)
208  [14]=>
209  int(3)
210}
211-=-=-=-=-=-=-=-=-=-= TEST 2 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
212$a=array (
213  'a' => 2,
214  'b' => 'some',
215  'c' => 'done',
216  'z' => 'foo',
217  'f' => 5,
218  'fan' => 'fen',
219  'bad' => 'bed',
220  'gate' => 'web',
221  7 => 18,
222  9 => 25,
223  11 => 42,
224  12 => 42,
225  45 => 42,
226  73 => 'foo',
227  95 => 'some',
228  'som3' => 'some',
229  'want' => 'wanna',
230);
231$b=array (
232  'a' => 7,
233  7 => 18,
234  9 => 13,
235  11 => 42,
236  45 => 46,
237  'som3' => 'some',
238  'foo' => 'some',
239  'goo' => 'foo',
240  'f' => 5,
241  'z' => 'equal',
242  'gate' => 'web',
243);
244$c=array (
245  'gate' => 'web',
246  73 => 'foo',
247  95 => 'some',
248);
249
250
251Results:
252
253array_intersect($a,$b,$c);
254array(6) {
255  ["b"]=>
256  string(4) "some"
257  ["z"]=>
258  string(3) "foo"
259  ["gate"]=>
260  string(3) "web"
261  [73]=>
262  string(3) "foo"
263  [95]=>
264  string(4) "some"
265  ["som3"]=>
266  string(4) "some"
267}
268array_intersect_assoc($a,$b,$c);
269array(1) {
270  ["gate"]=>
271  string(3) "web"
272}
273array_intersect($a,$b);
274array(11) {
275  ["b"]=>
276  string(4) "some"
277  ["z"]=>
278  string(3) "foo"
279  ["f"]=>
280  int(5)
281  ["gate"]=>
282  string(3) "web"
283  [7]=>
284  int(18)
285  [11]=>
286  int(42)
287  [12]=>
288  int(42)
289  [45]=>
290  int(42)
291  [73]=>
292  string(3) "foo"
293  [95]=>
294  string(4) "some"
295  ["som3"]=>
296  string(4) "some"
297}
298array_intersect_assoc($a,$b);
299array(5) {
300  ["f"]=>
301  int(5)
302  ["gate"]=>
303  string(3) "web"
304  [7]=>
305  int(18)
306  [11]=>
307  int(42)
308  ["som3"]=>
309  string(4) "some"
310}
311