-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12 Arrays Sorting.php
More file actions
60 lines (52 loc) · 1.5 KB
/
12 Arrays Sorting.php
File metadata and controls
60 lines (52 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<!-- PHP Sorting Arrays -->
<title>PHP Sorting Arrays | Sort Functions For Arrays</title>
<?php
// Indexed arrays - Arrays with a numeric index
$colors = array("Red", "Green", "Blue", "Black", "White");
foreach ($colors as $value) {
echo "$value <br>";
}
echo "<hr>";
#sort() - sort arrays in ascending order
sort($colors);
foreach ($colors as $value) {
echo "$value <br>";
}
echo "<hr>";
#rsort() - sort arrays in descending order
rsort($colors);
foreach ($colors as $value) {
echo "$value <br>";
}
echo "<hr>";
// Associative arrays - Arrays with named keys
$fruitsPrice = array("Papaya" => "85", "Apple" => "280", "Grapes" => "60", "Orange" => "100", "Mango" => "50");
foreach ($fruitsPrice as $key => $value) {
echo "$key : $value <br>";
}
echo "<hr>";
#asort() - sort associative arrays in ascending order, according to the value
asort($fruitsPrice);
foreach ($fruitsPrice as $key => $value) {
echo "$key : $value <br>";
}
echo "<hr>";
#ksort() - sort associative arrays in ascending order, according to the key
ksort($fruitsPrice);
foreach ($fruitsPrice as $key => $value) {
echo "$key : $value <br>";
}
echo "<hr>";
#arsort() - sort associative arrays in descending order, according to the value
arsort($fruitsPrice);
foreach ($fruitsPrice as $key => $value) {
echo "$key : $value <br>";
}
echo "<hr>";
#krsort() - sort associative arrays in descending order, according to the key
krsort($fruitsPrice);
foreach ($fruitsPrice as $key => $value) {
echo "$key : $value <br>";
}
echo "<hr>";
?>