1--TEST--
2Test array_intersect() function : usage variations - assoc array with diff keys for 'arr2' 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 $arr2 argument.
13 * The $arr1 argument is a fixed array.
14*/
15
16echo "*** Testing array_intersect() : assoc array with diff keys to \$arr2 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 $arr2 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 $arr1 argument
61$arr1 = 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 $arr2) {
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--EXPECT--
80*** Testing array_intersect() : assoc array with diff keys to $arr2 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  [0]=>
94  int(1)
95}
96array(1) {
97  [0]=>
98  int(1)
99}
100-- Iterator 4 --
101array(1) {
102  [0]=>
103  int(1)
104}
105array(1) {
106  [0]=>
107  int(1)
108}
109-- Iterator 5 --
110array(1) {
111  [1]=>
112  string(5) "float"
113}
114array(1) {
115  [1]=>
116  string(5) "float"
117}
118-- Iterator 6 --
119array(1) {
120  [2]=>
121  string(2) "f4"
122}
123array(1) {
124  [2]=>
125  string(2) "f4"
126}
127-- Iterator 7 --
128array(2) {
129  [4]=>
130  float(2.2)
131  [5]=>
132  string(5) "color"
133}
134array(2) {
135  [4]=>
136  float(2.2)
137  [5]=>
138  string(5) "color"
139}
140-- Iterator 8 --
141array(2) {
142  [4]=>
143  float(2.2)
144  [5]=>
145  string(5) "color"
146}
147array(2) {
148  [4]=>
149  float(2.2)
150  [5]=>
151  string(5) "color"
152}
153-- Iterator 9 --
154array(2) {
155  [3]=>
156  string(5) "hello"
157  [6]=>
158  string(6) "string"
159}
160array(2) {
161  [3]=>
162  string(5) "hello"
163  [6]=>
164  string(6) "string"
165}
166-- Iterator 10 --
167array(1) {
168  [3]=>
169  string(5) "hello"
170}
171array(1) {
172  [3]=>
173  string(5) "hello"
174}
175-- Iterator 11 --
176array(3) {
177  [0]=>
178  int(1)
179  [1]=>
180  string(5) "float"
181  [4]=>
182  float(2.2)
183}
184array(3) {
185  [0]=>
186  int(1)
187  [1]=>
188  string(5) "float"
189  [4]=>
190  float(2.2)
191}
192Done
193