1--TEST-- 2Test array_slice() function : usage variations - different data types as keys in an array 3--FILE-- 4<?php 5/* 6 * Pass different data types as keys in an array to array_slice() 7 * to test how $preserve_keys treats them 8 */ 9 10echo "*** Testing array_slice() : usage variations ***\n"; 11 12// Initialise function arguments not being substituted 13$offset = 0; 14$length = 10; // to ensure all elements are displayed 15 16//get an unset variable 17$unset_var = 10; 18unset ($unset_var); 19 20// heredoc string 21$heredoc = <<<EOT 22hello world 23EOT; 24 25// arrays of different data types to be passed as $input 26$inputs = array( 27 28 // int data 29/*1*/ 'int' => array( 30 0 => 'zero', 31 1 => 'one', 32 12345 => 'positive', 33 -2345 => 'negative', 34 ), 35 36 // null data 37/*4*/ 'null uppercase' => array( 38 NULL => 'null 1', 39 ), 40 41/*5*/ 'null lowercase' => array( 42 null => 'null 2', 43 ), 44 45 // boolean data 46/*6*/ 'bool lowercase' => array( 47 true => 'lowert', 48 false => 'lowerf', 49 ), 50 51/*7*/ 'bool uppercase' => array( 52 TRUE => 'uppert', 53 FALSE => 'upperf', 54 ), 55 56 // empty data 57/*8*/ 'empty double quotes' => array( 58 "" => 'emptyd', 59 ), 60 61/*9*/ 'empty single quotes' => array( 62 '' => 'emptys', 63 ), 64 65 // string data 66/*10*/ 'string' => array( 67 "stringd" => 'stringd', 68 'strings' => 'strings', 69 $heredoc => 'stringh', 70 ), 71 72 // undefined data 73/*11*/ 'undefined' => array( 74 @$undefined_var => 'undefined', 75 ), 76 77 // unset data 78/*12*/ 'unset' => array( 79 @$unset_var => 'unset', 80 ), 81); 82 83// loop through each element of $inputs to check the behavior of array_slice() 84$iterator = 1; 85foreach($inputs as $type => $input) { 86 echo "\n-- Iteration $iterator : key type is $type --\n"; 87 echo "\$preserve_keys = TRUE\n"; 88 var_dump( array_slice($input, $offset, $length, true) ); 89 echo "\$preserve_keys = FALSE\n"; 90 var_dump( array_slice($input, $offset, $length, false) ); 91 $iterator++; 92}; 93 94echo "Done"; 95?> 96--EXPECT-- 97*** Testing array_slice() : usage variations *** 98 99-- Iteration 1 : key type is int -- 100$preserve_keys = TRUE 101array(4) { 102 [0]=> 103 string(4) "zero" 104 [1]=> 105 string(3) "one" 106 [12345]=> 107 string(8) "positive" 108 [-2345]=> 109 string(8) "negative" 110} 111$preserve_keys = FALSE 112array(4) { 113 [0]=> 114 string(4) "zero" 115 [1]=> 116 string(3) "one" 117 [2]=> 118 string(8) "positive" 119 [3]=> 120 string(8) "negative" 121} 122 123-- Iteration 2 : key type is null uppercase -- 124$preserve_keys = TRUE 125array(1) { 126 [""]=> 127 string(6) "null 1" 128} 129$preserve_keys = FALSE 130array(1) { 131 [""]=> 132 string(6) "null 1" 133} 134 135-- Iteration 3 : key type is null lowercase -- 136$preserve_keys = TRUE 137array(1) { 138 [""]=> 139 string(6) "null 2" 140} 141$preserve_keys = FALSE 142array(1) { 143 [""]=> 144 string(6) "null 2" 145} 146 147-- Iteration 4 : key type is bool lowercase -- 148$preserve_keys = TRUE 149array(2) { 150 [1]=> 151 string(6) "lowert" 152 [0]=> 153 string(6) "lowerf" 154} 155$preserve_keys = FALSE 156array(2) { 157 [0]=> 158 string(6) "lowert" 159 [1]=> 160 string(6) "lowerf" 161} 162 163-- Iteration 5 : key type is bool uppercase -- 164$preserve_keys = TRUE 165array(2) { 166 [1]=> 167 string(6) "uppert" 168 [0]=> 169 string(6) "upperf" 170} 171$preserve_keys = FALSE 172array(2) { 173 [0]=> 174 string(6) "uppert" 175 [1]=> 176 string(6) "upperf" 177} 178 179-- Iteration 6 : key type is empty double quotes -- 180$preserve_keys = TRUE 181array(1) { 182 [""]=> 183 string(6) "emptyd" 184} 185$preserve_keys = FALSE 186array(1) { 187 [""]=> 188 string(6) "emptyd" 189} 190 191-- Iteration 7 : key type is empty single quotes -- 192$preserve_keys = TRUE 193array(1) { 194 [""]=> 195 string(6) "emptys" 196} 197$preserve_keys = FALSE 198array(1) { 199 [""]=> 200 string(6) "emptys" 201} 202 203-- Iteration 8 : key type is string -- 204$preserve_keys = TRUE 205array(3) { 206 ["stringd"]=> 207 string(7) "stringd" 208 ["strings"]=> 209 string(7) "strings" 210 ["hello world"]=> 211 string(7) "stringh" 212} 213$preserve_keys = FALSE 214array(3) { 215 ["stringd"]=> 216 string(7) "stringd" 217 ["strings"]=> 218 string(7) "strings" 219 ["hello world"]=> 220 string(7) "stringh" 221} 222 223-- Iteration 9 : key type is undefined -- 224$preserve_keys = TRUE 225array(1) { 226 [""]=> 227 string(9) "undefined" 228} 229$preserve_keys = FALSE 230array(1) { 231 [""]=> 232 string(9) "undefined" 233} 234 235-- Iteration 10 : key type is unset -- 236$preserve_keys = TRUE 237array(1) { 238 [""]=> 239 string(5) "unset" 240} 241$preserve_keys = FALSE 242array(1) { 243 [""]=> 244 string(5) "unset" 245} 246Done 247