-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray-review.php
50 lines (39 loc) · 1.13 KB
/
array-review.php
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
<?php
//PART 1
echo "<h3>Part 1</h3>";
$animals = array('panda', 'alpaca', 'boa'); //defined array
//function to sort and print the array
function sortAndPrint ($animals)
{
sort($animals);
for ($i = 0; $i <= sizeof($animals); $i++ )
{
echo ($animals[$i]." ");
}
}
//funtion to add an element
function addAnimal ($animals, $str)
{
echo "<br>adding " . $str . ". . .<br>";
$animals = array_map('strtolower', $animals);
$str = strtolower($str);
if (!in_array($str,$animals))
{
array_push($animals, $str);
}
sort($animals);
return $animals;
}
sortAndPrint ($animals);
$animals = addAnimal($animals, 'goat');
sortAndPrint($animals);
$animals = addAnimal($animals, 'Boa');
sortAndPrint($animals);
//part 2
echo "<h3>Part 2 </h3>";
$flavors = array("grasshoper"=>"The Grasshopper", "maple"=>"Whiskey Maple Bacon", "carrot"=>"Carrot Walnut", "caramel"=>"Salted Caramel Cupcake", "velvet"=>"Red Velvet", "lemon"=>"Lemon Drop", "tiramisu"=>"Tiramisu");
echo "<br>";
foreach ($flavors as $x => $x_value)
{
echo "<input type='checkbox' name='flavors[]' value='".$x."'> ". $x_value . "<br>";
}