Return to usort_variation3.phpt CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / standard / tests / array |
1.1 misho 1: --TEST-- 2: Test usort() function : usage variations - diff. array values 3: --FILE-- 4: <?php 5: /* Prototype : bool usort(array $array_arg, string $cmp_function) 6: * Description: Sort an array by values using a user-defined comparison function 7: * Source code: ext/standard/array.c 8: */ 9: 10: /* 11: * Pass an array with different data types as keys to usort() to test how it is re-ordered 12: */ 13: 14: echo "*** Testing usort() : usage variation ***\n"; 15: 16: function cmp_function($value1, $value2) 17: { 18: if($value1 == $value2) { 19: return 0; 20: } 21: else if($value1 > $value2) { 22: return -1; 23: } 24: else { 25: return 1; 26: } 27: } 28: 29: // different heredoc strings 30: 31: // single line heredoc string 32: $simple_heredoc = <<<EOT2 33: simple 34: EOT2; 35: 36: // multiline heredoc string 37: $multiline_heredoc = <<<EOT3 38: multiline heredoc with 123 39: and speci@! ch@r..\ncheck\talso 40: EOT3; 41: 42: $array_arg = array( 43: // numeric keys 44: -2 => 9, 45: 8.9 => 8, 46: 012 => 7, 47: 0x34 => 6, 48: 49: // string keys 50: 'key' => 5, //single quoted key 51: "two" => 4, //double quoted key 52: " " => 0, // space as key 53: 54: // bool keys 55: TRUE => 100, 56: FALSE => 25, 57: 58: // null keys 59: NULL => 35, 60: 61: // binary key 62: "a".chr(0)."b" => 45, 63: b"binary" => 30, 64: 65: //heredoc keys 66: $simple_heredoc => 75, 67: $multiline_heredoc => 200, 68: 69: // default key 70: 1, 71: ); 72: 73: var_dump( usort($array_arg, 'cmp_function') ); 74: echo "\n-- Sorted array after usort() function call --\n"; 75: var_dump($array_arg); 76: ?> 77: ===DONE=== 78: --EXPECTF-- 79: *** Testing usort() : usage variation *** 80: bool(true) 81: 82: -- Sorted array after usort() function call -- 83: array(15) { 84: [0]=> 85: int(200) 86: [1]=> 87: int(100) 88: [2]=> 89: int(75) 90: [3]=> 91: int(45) 92: [4]=> 93: int(35) 94: [5]=> 95: int(30) 96: [6]=> 97: int(25) 98: [7]=> 99: int(9) 100: [8]=> 101: int(8) 102: [9]=> 103: int(7) 104: [10]=> 105: int(6) 106: [11]=> 107: int(5) 108: [12]=> 109: int(4) 110: [13]=> 111: int(1) 112: [14]=> 113: int(0) 114: } 115: ===DONE===