-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_order.php
More file actions
executable file
·69 lines (50 loc) · 1.96 KB
/
array_order.php
File metadata and controls
executable file
·69 lines (50 loc) · 1.96 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
61
62
63
64
65
66
67
68
69
<!DOCTYPE html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<center>
<h1>Ordenando array:</h1>
<br/>
<h2>Digite quantos números quiser:</h2>
<form method="POST">
<input type="text" name="numbers" size="35" placeholder='Separe os números por (espaço) " " Ex: 1 2 3' />
<input type="submit" value="Ordenar." />
</form>
<hr/>
<?php
if (isset($_POST['numbers'])) {
# recebendo o input do form na variável input numbers que sera do tipo string
$input_numbers = $_POST['numbers'];
# convertendo a string em um array usando o (espaço) " " como delimitador
$array_numbers = explode(" ", $input_numbers);
echo "Você digitou: ";
print_r($input_numbers);
echo "<br/><br/>";
echo "Em ordem crescente: ";
# ordenando os itens do array do menor para o maior utilizando função nativa do php
sort($array_numbers, SORT_NUMERIC);
foreach ($array_numbers as $number => $value) {
echo $value;
# adiciona uma virgula "," entre os números e quando chegar no ultimo adiciona um ponto "."
if ($number < (count($array_numbers) -1)) {
echo ", ";
} else {
echo ". ";
}
}
echo "<br/><br/>";
echo "Em ordem decrescente: ";
rsort($array_numbers, SORT_NUMERIC);
foreach ($array_numbers as $number => $value) {
echo $value;
if ($number < (count($array_numbers) -1)) {
echo ", ";
} else {
echo ". ";
}
}
}
?>
</center>
</body>