Professional Documents
Culture Documents
home / study / engineering / computer science / computer science questions and answers / q2:...
Find solutions for your homework
Q2: Create an associative array of at least 15 elements with keys as registration numbers and values as
names of your class mates. Delete all the even records from the array. In the rest of the array, search the
value entered by user. If the value is found, print its key value.
Expert Answer
(sample data has been used, registration numbers and names can be modi ed as per the user's wish)
PHP code snippet for reference
<?php
//Data can be modi ed as per user's preference. This is sample data
$class = array(
12 => 'Peter',
3 => 'Ben',
9 => 'Joe',
6 => 'Harry',
1 => 'Liam',
15 => 'Noah',
13 => 'Oliver',
10 => 'William',
4 => 'James',
14 => 'Benjamin',
8 => 'Lucas',
11 => 'Henry',
5 => 'Isabella',
2 => 'Mia',
7 => 'Sophia',
);
//To delete even records
foreach($class as $index => $index_value) {
if($index % 2 == 0) {
unset($class[$index]);
}
}
echo nl2br("Array after deleting even records\n\n");
//Print associative array
foreach($class as $x => $x_value) {
echo "Registration Number = " . $x . ", Name = " . $x_value;
echo nl2br("\n");
}
Output
COMPANY
CHEGG NETWORK
CUSTOMER SERVICE