-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.php
209 lines (138 loc) · 4.25 KB
/
array.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<p style="color: red;">Create an array representing your family members</p>
<?php $family_members = array(
0 => 'Galina',
1 => 'Alina',
2 => 'Ksenia',
3 => 'Eric',
); ?>
<p>My family consists of
<?php
print_r($family_members);
?>. </p>
<hr>
<?php
var_dump($family_members);
?>
<hr>
<p style="color: red;">Retrieve and display an element</p>
<?php
echo $family_members[2];
?>
<hr>
<p style="color: red;">Add an element</p>
<?php
array_push($family_members, 'Julia');
print_r($family_members);
?>
<hr>
<p style="color: red;">And the short way :</p>
<?php
$family_members[] = 'Julia';
print_r($family_members);
?>
<hr>
<p style="color: red;">If you want to use an associative key (more on this in a bit):</p>
<?php
$person['function'] = 'Junior Web Developer';
print_r($family_members);
?>
<hr>
<p style="color: red;">Replace the value of a key by another one. Say $person describes, Jeanne, a Junior Web Developer :</p>
<?php
$person['function'] = 'Junior Web Developer';
$person['function'] = 'Junior Web Developer';
echo $person['function'];
// returns 'Junior Web Developer'
?>
<hr>
<p style="color: red;">After a few years in the industry, she's promoted to Senior.</p>
<?php
$person['function'] = 'Junior Web Developer';
$person['function'] = 'Senior Web Developer';
echo $person['function'];
// returns 'Senior Web Developer'
?>
<hr>
<p style="color: red;">Associative Array.</p>
<?php
$person[0] = '02/2198445';
$person[1] = 'Cantersteen 10';
$person[2] = 'Brussels';
// compare this
echo $person[1];
// with this
echo $person['street'];
?>
hr>
<p style="color: red;">Coding an Associative Array.</p>
<?php
$user = array(
'firstname' => 'Juan',
'lastname' => 'Pablo',
'adress' => '3 Paradijsestraat',
'city' => 'Antwerpen'
);
echo $user['lastname'];
?>
<p style="color: red;">Exercise
Describe yourself using an associative array: $me. Specify your age, your favourite season of the year, wether you like soccer or not (boolean). Try to use the right variable type for each value.</p>
Add your hobbies using an array to your $me array.
<?php
$me = array('firstname' => 'Julia', 'age' => '35', 'favourite season' => 'summer', 'soccer' => true, 'hobbies' => array('dance', 'politics', 'culture'));
echo '<pre>';
print_r($me);
echo '</pre>';
?>
<p style="color: red;">Then, after the $me array, create a second array with the same keys as yours, to describe your mother : the $mother array (make it someone else if you prefer).
<?php
$me = array('firstname' => 'Julia', 'age' => '35', 'favourite season' => 'summer', 'soccer' => true, 'hobbies' => array('dance', 'politics', 'culture'));
$alina = array('firstname' => 'Alina', 'age' => '8', 'favourite season' => 'summer', 'soccer' => true, 'hobbies' => array('guitar', 'gymnastics', 'playing'));
echo '<pre>';
print_r($me);
print_r($alina);
echo '</pre>';
?>
<p style="color: red;">Then, below that, assign the $mother array to a new key in your $mearray : let's call that key 'mother'.</p>
<?php
echo '<pre>';
array_push($me, $alina);
$me['alina'] = $me[0];
unset($me[0]);
print_r($me);
echo '</pre>';
?>
<p style="color: red;">Say you want to know how many hobbies your mother has. Try to guess the native php function that allows you to count the number of elements in an array.</p>
<?php
echo '<pre>';
var_dump(count($me['alina']['hobbies']));
echo '</pre>';
?>
<p style="color: red;">Below, count your own hobbies</p>
<?php
echo '<pre>';
var_dump(count($me['hobbies']));
echo '</pre>';
?>
<p style="color: red;">Below, add both totals and display the total amount of hobbies.</p>
<?php
echo '<pre>';
$a = number_format(var_dump(count($me['hobbies'])));
$b = number_format(var_dump(count($me['alina']['hobbies'])));
echo ($a + $b);
echo '</pre>';
?>
<p style="color: red;">A friend introduced you to the joys of Taxidermy : it quickly becomes your new hobby. Try to find the right way to add that new hobby to your array's 'hobbies' key.</p>
<?php
echo '<pre>';
$me['hobbies'][] = 'taxidermy';
print_r($me);
echo '</pre>';
?>
<p style="color: red;">Replace
You decide you need a reset and change your firstname to Lucy. How would you update your array ?</p>
<?php
echo '<pre>';
$me['firstname'] = 'Lucy';
print_r($me);
echo '</pre>';
?>