Annotation of embedaddon/php/ext/standard/tests/array/array_intersect_variation5.phpt, revision 1.1.1.1

1.1       misho       1: --TEST--
                      2: Test 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: 
                     16: echo "*** Testing array_intersect() : assoc array with diff keys to \$arr1 argument ***\n";
                     17: 
                     18: // get an unset variable
                     19: $unset_var = 10;
                     20: unset ($unset_var);
                     21: 
                     22: // get a heredoc string
                     23: $heredoc = <<<EOT
                     24: Hello world
                     25: EOT;
                     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;
                     65: foreach($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: 
                     77: echo "Done";
                     78: ?>
                     79: --EXPECTF--
                     80: *** Testing array_intersect() : assoc array with diff keys to $arr1 argument ***
                     81: -- Iterator 1 --
                     82: array(0) {
                     83: }
                     84: array(0) {
                     85: }
                     86: -- Iterator 2 --
                     87: array(0) {
                     88: }
                     89: array(0) {
                     90: }
                     91: -- Iterator 3 --
                     92: array(1) {
                     93:   [1]=>
                     94:   string(1) "1"
                     95: }
                     96: array(1) {
                     97:   [1]=>
                     98:   string(1) "1"
                     99: }
                    100: -- Iterator 4 --
                    101: array(1) {
                    102:   [1]=>
                    103:   string(1) "1"
                    104: }
                    105: array(1) {
                    106:   [1]=>
                    107:   string(1) "1"
                    108: }
                    109: -- Iterator 5 --
                    110: array(1) {
                    111:   [2]=>
                    112:   string(5) "float"
                    113: }
                    114: array(1) {
                    115:   [2]=>
                    116:   string(5) "float"
                    117: }
                    118: -- Iterator 6 --
                    119: array(1) {
                    120:   [33333333]=>
                    121:   string(2) "f4"
                    122: }
                    123: array(1) {
                    124:   [33333333]=>
                    125:   string(2) "f4"
                    126: }
                    127: -- Iterator 7 --
                    128: array(2) {
                    129:   ["re\td"]=>
                    130:   string(5) "color"
                    131:   ["\v\fworld"]=>
                    132:   float(2.2)
                    133: }
                    134: array(2) {
                    135:   ["re\td"]=>
                    136:   string(5) "color"
                    137:   ["\v\fworld"]=>
                    138:   float(2.2)
                    139: }
                    140: -- Iterator 8 --
                    141: array(2) {
                    142:   ["re d"]=>
                    143:   string(5) "color"
                    144:   ["world"]=>
                    145:   float(2.2)
                    146: }
                    147: array(2) {
                    148:   ["re d"]=>
                    149:   string(5) "color"
                    150:   ["world"]=>
                    151:   float(2.2)
                    152: }
                    153: -- Iterator 9 --
                    154: array(2) {
                    155:   [0]=>
                    156:   string(5) "hello"
                    157:   ["Hello world"]=>
                    158:   string(6) "string"
                    159: }
                    160: array(2) {
                    161:   [0]=>
                    162:   string(5) "hello"
                    163:   ["Hello world"]=>
                    164:   string(6) "string"
                    165: }
                    166: -- Iterator 10 --
                    167: array(1) {
                    168:   [""]=>
                    169:   string(5) "hello"
                    170: }
                    171: array(1) {
                    172:   [""]=>
                    173:   string(5) "hello"
                    174: }
                    175: -- Iterator 11 --
                    176: array(3) {
                    177:   ["hello"]=>
                    178:   int(1)
                    179:   ["fruit"]=>
                    180:   float(2.2)
                    181:   [444]=>
                    182:   string(5) "float"
                    183: }
                    184: array(3) {
                    185:   ["hello"]=>
                    186:   int(1)
                    187:   ["fruit"]=>
                    188:   float(2.2)
                    189:   [444]=>
                    190:   string(5) "float"
                    191: }
                    192: Done

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>