Return to uasort_basic1.phpt CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / standard / tests / array |
1.1 misho 1: --TEST-- 2: Test uasort() function : basic functionality 3: --FILE-- 4: <?php 5: /* Prototype : bool uasort(array $array_arg, string $cmp_function) 6: * Description: Sort an array with a user-defined comparison function and maintain index association 7: * Source code: ext/standard/array.c 8: */ 9: 10: echo "*** Testing uasort() : basic functionality ***\n"; 11: 12: // comparison function 13: /* Prototype : int cmp(mixed $value1, mixed $value2) 14: * Parameters : $value1 and $value2 - values to be compared 15: * Return value : 0 - if both values are same 16: * 1 - if value1 is greater than value2 17: * -1 - if value1 is less than value2 18: * Description : compares value1 and value2 19: */ 20: function cmp($value1, $value2) 21: { 22: if($value1 == $value2) { 23: return 0; 24: } 25: else if($value1 > $value2) { 26: return 1; 27: } 28: else 29: return -1; 30: } 31: 32: // Int array with default keys 33: $int_values = array(1, 8, 9, 3, 2, 6, 7); 34: echo "-- Numeric array with default keys --\n"; 35: var_dump( uasort($int_values, 'cmp') ); 36: var_dump($int_values); 37: 38: // String array with default keys 39: $string_values = array("This", "is", 'a', "test"); 40: echo "-- String array with default keys --\n"; 41: var_dump( uasort($string_values, 'cmp') ); 42: var_dump($string_values); 43: 44: // Associative array with numeric keys 45: $numeric_key_arg = array(1=> 1, 2 => 2, 3 => 7, 5 => 4, 4 => 9); 46: echo "-- Associative array with numeric keys --\n"; 47: var_dump( uasort($numeric_key_arg, 'cmp') ); 48: var_dump($numeric_key_arg); 49: 50: // Associative array with string keys 51: $string_key_arg = array('one' => 4, 'two' => 2, 'three' => 1, 'four' => 10); 52: echo "-- Associative array with string keys --\n"; 53: var_dump( uasort($string_key_arg, 'cmp') ); 54: var_dump($string_key_arg); 55: 56: echo "Done" 57: ?> 58: --EXPECTF-- 59: *** Testing uasort() : basic functionality *** 60: -- Numeric array with default keys -- 61: bool(true) 62: array(7) { 63: [0]=> 64: int(1) 65: [4]=> 66: int(2) 67: [3]=> 68: int(3) 69: [5]=> 70: int(6) 71: [6]=> 72: int(7) 73: [1]=> 74: int(8) 75: [2]=> 76: int(9) 77: } 78: -- String array with default keys -- 79: bool(true) 80: array(4) { 81: [0]=> 82: string(4) "This" 83: [2]=> 84: string(1) "a" 85: [1]=> 86: string(2) "is" 87: [3]=> 88: string(4) "test" 89: } 90: -- Associative array with numeric keys -- 91: bool(true) 92: array(5) { 93: [1]=> 94: int(1) 95: [2]=> 96: int(2) 97: [5]=> 98: int(4) 99: [3]=> 100: int(7) 101: [4]=> 102: int(9) 103: } 104: -- Associative array with string keys -- 105: bool(true) 106: array(4) { 107: ["three"]=> 108: int(1) 109: ["two"]=> 110: int(2) 111: ["one"]=> 112: int(4) 113: ["four"]=> 114: int(10) 115: } 116: Done