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