1--TEST-- 2Test uasort() function : usage variations - sort array with all posible keys 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/* 11* Testing uasort() with 'array_arg' having different keys 12*/ 13 14echo "*** Testing uasort() : Sorting array with all possible keys ***\n"; 15 16//comparison function 17/* Prototype : int cmp_function(mixed $value1, mixed $value2) 18 * Parameters : $value1 and $value2 - values to be compared 19 * Return value : 0 - if both values are same 20 * 1 - if value1 is greater than value2 21 * -1 - if value1 is less than value2 22 * Description : compares value1 and value2 23 */ 24function cmp_function($value1, $value2) 25{ 26 if($value1 == $value2) { 27 return 0; 28 } 29 else if($value1 > $value2) { 30 return -1; 31 } 32 else { 33 return 1; 34 } 35} 36 37// different heredoc strings 38//empty heredoc string 39$empty_heredoc = <<<EOT1 40EOT1; 41 42// single line heredoc string 43$simple_heredoc = <<<EOT2 44simple 45EOT2; 46 47// multiline heredoc string 48$multiline_heredoc = <<<EOT3 49multiline heredoc with 123 50and speci@! ch@r..\ncheck\talso 51EOT3; 52 53$array_arg = array( 54 // default key 55 1, //expecting: default key 0, value will be replaced by 'FALSE' 56 57 // numeric keys 58 1 => 10, // expecting: value will be replaced by 'TRUE' 59 -2 => 9, 60 8.9 => 8, 61 012 => 7, 62 0x34 => 6, 63 64 // string keys 65 'key' => 5, //single quoted key 66 "two" => 4, //double quoted key 67 '' => 3, 68 "" => 2, 69 " " => 0, // space as key 70 71 // bool keys 72 true => 15, 73 false => 5, 74 TRUE => 100, 75 FALSE => 25, 76 77 // null keys 78 null => 20, // expecting: value will be replaced by 'NULL' 79 NULL => 35, 80 81 // binary key 82 "a".chr(0)."b" => 45, 83 b"binary" => 30, 84 85 //heredoc keys 86 $empty_heredoc => 90, 87 $simple_heredoc => 75, 88 $multiline_heredoc => 200, 89); 90 91var_dump( uasort($array_arg, 'cmp_function') ); 92echo "-- Sorted array after uasort() function call --\n"; 93var_dump($array_arg); 94 95echo "Done" 96?> 97--EXPECTF-- 98*** Testing uasort() : Sorting array with all possible keys *** 99bool(true) 100-- Sorted array after uasort() function call -- 101array(14) { 102 ["multiline heredoc with 123 103and speci@! ch@r.. 104check also"]=> 105 int(200) 106 [1]=> 107 int(100) 108 [""]=> 109 int(90) 110 ["simple"]=> 111 int(75) 112 ["ab"]=> 113 int(45) 114 ["binary"]=> 115 int(30) 116 [0]=> 117 int(25) 118 [-2]=> 119 int(9) 120 [8]=> 121 int(8) 122 [10]=> 123 int(7) 124 [52]=> 125 int(6) 126 ["key"]=> 127 int(5) 128 ["two"]=> 129 int(4) 130 [" "]=> 131 int(0) 132} 133Done 134