1--TEST--
2Test array_intersect() function : usage variations - assoc array with diff keys for 'arr1' argument
3--FILE--
4<?php
5/* Prototype  : array array_intersect(array $arr1, array $arr2 [, array $...])
6 * Description: Returns the entries of arr1 that have values which are present in all the other arguments
7 * Source code: ext/standard/array.c
8*/
9
10/*
11 * Testing the functionality of array_intersect() by passing different
12 * associative arrays having different possible keys to $arr1 argument.
13 * The $arr2 argument is a fixed array
14*/
15
16echo "*** Testing array_intersect() : assoc array with diff keys to \$arr1 argument ***\n";
17
18// get an unset variable
19$unset_var = 10;
20unset ($unset_var);
21
22// get a heredoc string
23$heredoc = <<<EOT
24Hello world
25EOT;
26
27// different variations of associative arrays to be passed to $arr1 argument
28$arrays = array (
29
30       // empty array
31/*1*/  array(),
32
33       // arrays with integer keys
34       array(0 => "0"),
35       array(1 => "1"),
36       array(1 => "1", 2 => "2", 3 => "3", 4 => "4"),
37
38       // arrays with float keys
39/*5*/  array(2.3333 => "float"),
40       array(1.2 => "f1", 3.33 => "f2",
41             4.89999922839999 => "f3",
42             33333333.333333 => "f4"),
43
44       // arrays with string keys
45/*7*/  array('\tHello' => 111, 're\td' => "color",
46             '\v\fworld' => 2.2, 'pen\n' => 33),
47       array("\tHello" => 111, "re\td" => "color",
48             "\v\fworld" => 2.2, "pen\n" => 33),
49       array("hello", $heredoc => "string"), // heredoc
50
51       // array with unset variable
52/*10*/ array( @$unset_var => "hello"),
53
54       // array with mixed keys
55/*11*/ array('hello' => 1,  "fruit" => 2.2,
56              133 => "int", 444.432 => "float",
57             @$unset_var => "unset", $heredoc => "heredoc")
58);
59
60// array to be passsed to $arr2 argument
61$arr2 = array(1, "float", "f4", "hello", 2.2, 'color', "string", "pen\n", 11);
62
63// loop through each sub-array within $arrrays to check the behavior of array_intersect()
64$iterator = 1;
65foreach($arrays as $arr1) {
66  echo "-- Iterator $iterator --\n";
67
68  // Calling array_intersect() with default arguments
69  var_dump( array_intersect($arr1, $arr2) );
70
71  // Calling array_intersect() with more arguments.
72  // additional argument passed is the same as $arr1 argument
73  var_dump( array_intersect($arr1, $arr2, $arr1) );
74  $iterator++;
75}
76
77echo "Done";
78?>
79--EXPECTF--
80*** Testing array_intersect() : assoc array with diff keys to $arr1 argument ***
81-- Iterator 1 --
82array(0) {
83}
84array(0) {
85}
86-- Iterator 2 --
87array(0) {
88}
89array(0) {
90}
91-- Iterator 3 --
92array(1) {
93  [1]=>
94  string(1) "1"
95}
96array(1) {
97  [1]=>
98  string(1) "1"
99}
100-- Iterator 4 --
101array(1) {
102  [1]=>
103  string(1) "1"
104}
105array(1) {
106  [1]=>
107  string(1) "1"
108}
109-- Iterator 5 --
110array(1) {
111  [2]=>
112  string(5) "float"
113}
114array(1) {
115  [2]=>
116  string(5) "float"
117}
118-- Iterator 6 --
119array(1) {
120  [33333333]=>
121  string(2) "f4"
122}
123array(1) {
124  [33333333]=>
125  string(2) "f4"
126}
127-- Iterator 7 --
128array(2) {
129  ["re\td"]=>
130  string(5) "color"
131  ["\v\fworld"]=>
132  float(2.2)
133}
134array(2) {
135  ["re\td"]=>
136  string(5) "color"
137  ["\v\fworld"]=>
138  float(2.2)
139}
140-- Iterator 8 --
141array(2) {
142  ["re	d"]=>
143  string(5) "color"
144  ["world"]=>
145  float(2.2)
146}
147array(2) {
148  ["re	d"]=>
149  string(5) "color"
150  ["world"]=>
151  float(2.2)
152}
153-- Iterator 9 --
154array(2) {
155  [0]=>
156  string(5) "hello"
157  ["Hello world"]=>
158  string(6) "string"
159}
160array(2) {
161  [0]=>
162  string(5) "hello"
163  ["Hello world"]=>
164  string(6) "string"
165}
166-- Iterator 10 --
167array(1) {
168  [""]=>
169  string(5) "hello"
170}
171array(1) {
172  [""]=>
173  string(5) "hello"
174}
175-- Iterator 11 --
176array(3) {
177  ["hello"]=>
178  int(1)
179  ["fruit"]=>
180  float(2.2)
181  [444]=>
182  string(5) "float"
183}
184array(3) {
185  ["hello"]=>
186  int(1)
187  ["fruit"]=>
188  float(2.2)
189  [444]=>
190  string(5) "float"
191}
192Done
193