1--TEST-- 2Test array_intersect() function : usage variations - assoc array with diff values for 'arr2' argument 3--FILE-- 4<?php 5/* 6 * Testing the functionality of array_intersect() by passing different 7 * associative arrays having different possible values to $arr2 argument. 8 * The $arr1 argument is a fixed array. 9*/ 10 11echo "*** Testing array_intersect() : assoc array with diff values to \$arr2 argument ***\n"; 12 13// get an unset variable 14$unset_var = 10; 15unset ($unset_var); 16 17// get a resource variable 18$fp = fopen(__FILE__, "r"); 19 20// get a class 21class classA 22{ 23 public function __toString(){ 24 return "Class A object"; 25 } 26} 27 28// get a heredoc string 29$heredoc = <<<EOT 30Hello world 31EOT; 32 33// different variations of associative arrays to be passed to $arr2 argument 34$arrays = array ( 35 36 // empty array 37/*1*/ array(), 38 39 // arrays with integer values 40 array('0' => 0), 41 array("1" => 1), 42 array("one" => 1, 'two' => 2, "three" => 3, 4 => 4), 43 44 // arrays with float values 45/*5*/ array("float" => 2.3333), 46 array("f1" => 1.2, 'f2' => 3.33, 3 => 4.89999922839999, 'f4' => 33333333.333333), 47 48 // arrays with string values 49/*7*/ array(111 => "\tHello", "red" => "col\tor", 2 => "\v\fworld", 3.3 => "pen\n"), 50 array(111 => '\tHello', "red" => 'col\tor', 2 => '\v\fworld', 3.3 => 'pen\n'), 51 array(1 => "hello", "heredoc" => $heredoc), 52 53 // array with object, unset variable and resource variable 54/*10*/ array(11 => new classA(), "unset" => @$unset_var, "resource" => $fp), 55 56 // array with mixed values 57/*11*/ array(1 => 'hello', 2 => new classA(), 222 => "fruit", 58 'resource' => $fp, "int" => 133, "float" => 444.432, 59 "unset" => @$unset_var, "heredoc" => $heredoc) 60); 61 62// array to be passsed to $arr1 argument 63$arr1 = array(1, 2, 1.2, 2.3333, "col\tor", '\v\fworld', $fp, 64 "Hello world", $heredoc, new classA(), 444.432, "fruit"); 65 66// loop through each sub-array within $arrays to check the behavior of array_intersect() 67$iterator = 1; 68foreach($arrays as $arr2) { 69 echo "-- Iterator $iterator --\n"; 70 71 // Calling array_intersect() with default arguments 72 var_dump( array_intersect($arr1, $arr2) ); 73 74 // Calling array_intersect() with more arguments. 75 // additional argument passed is the same as $arr1 argument 76 var_dump( array_intersect($arr1, $arr2, $arr1) ); 77 $iterator++; 78} 79 80// close the file resource used 81fclose($fp); 82 83echo "Done"; 84?> 85--EXPECTF-- 86*** Testing array_intersect() : assoc array with diff values to $arr2 argument *** 87-- Iterator 1 -- 88array(0) { 89} 90array(0) { 91} 92-- Iterator 2 -- 93array(0) { 94} 95array(0) { 96} 97-- Iterator 3 -- 98array(1) { 99 [0]=> 100 int(1) 101} 102array(1) { 103 [0]=> 104 int(1) 105} 106-- Iterator 4 -- 107array(2) { 108 [0]=> 109 int(1) 110 [1]=> 111 int(2) 112} 113array(2) { 114 [0]=> 115 int(1) 116 [1]=> 117 int(2) 118} 119-- Iterator 5 -- 120array(1) { 121 [3]=> 122 float(2.3333) 123} 124array(1) { 125 [3]=> 126 float(2.3333) 127} 128-- Iterator 6 -- 129array(1) { 130 [2]=> 131 float(1.2) 132} 133array(1) { 134 [2]=> 135 float(1.2) 136} 137-- Iterator 7 -- 138array(1) { 139 [4]=> 140 string(6) "col or" 141} 142array(1) { 143 [4]=> 144 string(6) "col or" 145} 146-- Iterator 8 -- 147array(1) { 148 [5]=> 149 string(9) "\v\fworld" 150} 151array(1) { 152 [5]=> 153 string(9) "\v\fworld" 154} 155-- Iterator 9 -- 156array(2) { 157 [7]=> 158 string(11) "Hello world" 159 [8]=> 160 string(11) "Hello world" 161} 162array(2) { 163 [7]=> 164 string(11) "Hello world" 165 [8]=> 166 string(11) "Hello world" 167} 168-- Iterator 10 -- 169array(2) { 170 [6]=> 171 resource(%d) of type (stream) 172 [9]=> 173 object(classA)#%d (0) { 174 } 175} 176array(2) { 177 [6]=> 178 resource(%d) of type (stream) 179 [9]=> 180 object(classA)#%d (0) { 181 } 182} 183-- Iterator 11 -- 184array(6) { 185 [6]=> 186 resource(%d) of type (stream) 187 [7]=> 188 string(11) "Hello world" 189 [8]=> 190 string(11) "Hello world" 191 [9]=> 192 object(classA)#%d (0) { 193 } 194 [10]=> 195 float(444.432) 196 [11]=> 197 string(5) "fruit" 198} 199array(6) { 200 [6]=> 201 resource(%d) of type (stream) 202 [7]=> 203 string(11) "Hello world" 204 [8]=> 205 string(11) "Hello world" 206 [9]=> 207 object(classA)#%d (0) { 208 } 209 [10]=> 210 float(444.432) 211 [11]=> 212 string(5) "fruit" 213} 214Done 215