Skip to content

Commit f286975

Browse files
authored
updated README.md
1 parent 2c6e286 commit f286975

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

README.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,58 @@
1-
# php-arrays
1+
# PHP Arrays
2+
Sort an array of temperatures in ascending order and retrieve 5 coolest and 5 warmest temperatures.
3+
4+
### Initialize array
5+
`$array = array(68, 70, 72, 58, 60, 79, 82, 73, 75, 77, 73, 58, 63, 79, 78, 68, 72, 73, 80, 79, 68, 72, 75, 77, 73, 78, 82, 85, 89, 83);`
6+
7+
### Print array
8+
`print_r($array);`
9+
10+
#### Output:
11+
![printArray](https://user-images.githubusercontent.com/32812640/82562015-0a62d080-9b92-11ea-8490-95c4582b636c.PNG)
12+
13+
### Sort array in ascending order
14+
<pre>
15+
for ($j = 0; $j < count($array); $j++) {
16+
for ($i = 0; $i < count($array) - 1; $i++) {
17+
if ($array[$i] > $array[$i + 1]) {
18+
$temp = $array[$i + 1];
19+
$array[$i + 1] = $array[$i];
20+
$array[$i] = $temp;
21+
}
22+
}
23+
}
24+
</pre>
25+
26+
### Average of all tempratures
27+
<pre>
28+
$total = 0;
29+
foreach ($array as $key => $value) {
30+
$total = $total + $value;
31+
}
32+
echo ($total / count($array)) . ' is the average of the temperatures.';
33+
</pre>
34+
35+
#### Output:
36+
![average](https://user-images.githubusercontent.com/32812640/82561574-3d589480-9b91-11ea-9c6c-ee15a22c8802.PNG)
37+
38+
### Five coolest tempratures
39+
<pre>
40+
$max = count($array);
41+
for ($i = $max - 5; $i < $max; $i++) {
42+
echo "$array[$i]&#8457; temperature";
43+
}
44+
</pre>
45+
46+
#### Output:
47+
![coolest](https://user-images.githubusercontent.com/32812640/82561782-958f9680-9b91-11ea-8389-11f4e350b07e.PNG)
48+
49+
50+
### Five warmest tempratures
51+
<pre>
52+
for ($i = 0; $i < 5; $i++) {
53+
echo "$array[$i]&#8457; temperature";
54+
}
55+
</pre>
56+
57+
#### Output:
58+
![warmest](https://user-images.githubusercontent.com/32812640/82561725-798bf500-9b91-11ea-8094-924e9d476215.PNG)

0 commit comments

Comments
 (0)