https://github.com/chiragobhan/php-arrays
Sort an array of temperatures in ascending order and retrieve 5 coolest and 5 warmest temperatures.
https://github.com/chiragobhan/php-arrays
arrays arraysort php retrieve-data sorting temperature
Last synced: 9 months ago
JSON representation
Sort an array of temperatures in ascending order and retrieve 5 coolest and 5 warmest temperatures.
- Host: GitHub
- URL: https://github.com/chiragobhan/php-arrays
- Owner: chiragobhan
- Created: 2020-05-21T12:29:22.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-21T13:12:58.000Z (about 6 years ago)
- Last Synced: 2025-06-28T06:41:44.184Z (12 months ago)
- Topics: arrays, arraysort, php, retrieve-data, sorting, temperature
- Language: PHP
- Homepage:
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# PHP Arrays
Sort an array of temperatures in ascending order and retrieve 5 coolest and 5 warmest temperatures.
### Initialize array
`$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);`
### Print array
`print_r($array);`
#### Output:

### Sort array in ascending order
for ($j = 0; $j < count($array); $j++) {
for ($i = 0; $i < count($array) - 1; $i++) {
if ($array[$i] > $array[$i + 1]) {
$temp = $array[$i + 1];
$array[$i + 1] = $array[$i];
$array[$i] = $temp;
}
}
}
### Average of all tempratures
$total = 0;
foreach ($array as $key => $value) {
$total = $total + $value;
}
echo ($total / count($array)) . ' is the average of the temperatures.';
#### Output:

### Five coolest tempratures
$max = count($array);
for ($i = $max - 5; $i < $max; $i++) {
echo "$array[$i]℉ temperature";
}
#### Output:

### Five warmest tempratures
for ($i = 0; $i < 5; $i++) {
echo "$array[$i]℉ temperature";
}
#### Output:
