~ubuntu-branches/ubuntu/quantal/php5/quantal

« back to all changes in this revision

Viewing changes to ext/standard/tests/array/usort_variation10.phpt

  • Committer: Bazaar Package Importer
  • Author(s): Sean Finney
  • Date: 2009-07-01 09:12:10 UTC
  • mto: (0.9.1) (1.1.17 upstream)
  • mto: This revision was merged to the branch mainline in revision 58.
  • Revision ID: james.westby@ubuntu.com-20090701091210-go0h6506p62on17r
Tags: upstream-5.3.0
ImportĀ upstreamĀ versionĀ 5.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
--TEST--
 
2
Test usort() function : usage variations - duplicate keys and 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 duplicate keys and values to usort() to test behaviour
 
12
 */
 
13
 
 
14
echo "*** Testing usort() : usage variation ***\n";
 
15
 
 
16
function cmp($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
// Array with duplicate string and integer keys and values
 
29
$array_arg = array(0 => 2,     "a" => 8, "d" => 9, 
 
30
                   3 => 3,     5 => 2,   "o" => 6, 
 
31
                   "z" => -99, 0 => 1,   "z" => 3);
 
32
 
 
33
echo "\n-- Array with duplicate keys --\n";
 
34
var_dump( usort($array_arg, 'cmp') );
 
35
var_dump($array_arg);
 
36
 
 
37
// Array with default and assigned keys
 
38
$array_arg = array(0 => "Banana", 1 => "Mango", "Orange", 2 => "Apple", "Pineapple");
 
39
 
 
40
echo "\n-- Array with default/assigned keys --\n";
 
41
var_dump( usort($array_arg, 'cmp') );
 
42
var_dump($array_arg);
 
43
?>
 
44
===DONE===
 
45
--EXPECTF--
 
46
*** Testing usort() : usage variation ***
 
47
 
 
48
-- Array with duplicate keys --
 
49
bool(true)
 
50
array(7) {
 
51
  [0]=>
 
52
  int(1)
 
53
  [1]=>
 
54
  int(2)
 
55
  [2]=>
 
56
  int(3)
 
57
  [3]=>
 
58
  int(3)
 
59
  [4]=>
 
60
  int(6)
 
61
  [5]=>
 
62
  int(8)
 
63
  [6]=>
 
64
  int(9)
 
65
}
 
66
 
 
67
-- Array with default/assigned keys --
 
68
bool(true)
 
69
array(4) {
 
70
  [0]=>
 
71
  string(5) "Apple"
 
72
  [1]=>
 
73
  string(6) "Banana"
 
74
  [2]=>
 
75
  string(5) "Mango"
 
76
  [3]=>
 
77
  string(9) "Pineapple"
 
78
}
 
79
===DONE===