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