You are on page 1of 4

Alert when this question is answered

× Download the Chegg Study App GE T


★★★★★ (80K+)

  Textbook Solutions Expert Q&A Practice 

home / study / engineering / computer science / computer science questions and answers / q2:...
Find solutions for your homework

Question: Q2: Create an associative array of at least 15


elements with keys as registration numbers and val...

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

Rajat Singh Sandhu


answered this

Consider the following PHP code snippet

(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");
}

$input = readline('Enter a key value: ');


$key = array_search($input,$class);
echo nl2br("\n");
echo "Key of entered key value is " . $key;
?>

Output

(<br> indicates line break)


0 Comments

Was this answer helpful? 1

COMPANY

LEGAL & POLICIES

CHEGG PRODUCTS AND SERVICES

CHEGG NETWORK

CUSTOMER SERVICE

© 2003-2021 Chegg Inc. All rights reserved.

You might also like