0% found this document useful (0 votes)
11 views

Associative array in php

The document provides PHP scripts demonstrating various operations on associative arrays, including creation, modification, iteration, counting elements, checking for keys, removing keys, sorting, merging, searching for values, and reversing the array. Each operation is illustrated with code examples for clarity. The examples cover fundamental array manipulations essential for PHP programming.

Uploaded by

bejpapai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Associative array in php

The document provides PHP scripts demonstrating various operations on associative arrays, including creation, modification, iteration, counting elements, checking for keys, removing keys, sorting, merging, searching for values, and reversing the array. Each operation is illustrated with code examples for clarity. The examples cover fundamental array manipulations essential for PHP programming.

Uploaded by

bejpapai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Associative array in php

1. Create and Print an Associative Array


Write a PHP script to create an associative array of 5 students with their marks and
print each key-value pair.
<?php
$students = [ "John" => 85, "Emma" => 90, "Mike" => 78, "Sophia" => 92, "David" => 88];
foreach ($students as $name => $marks) {
echo "Student: $name, Marks: $marks <br>";
}
?>

2. Access and Modify Values in an Associative Array


Write a PHP script to modify the marks of a specific student in an associative array
and print the updated array.
<?php
$students = ["John" => 85, "Emma" => 90, "Mike" => 78];
$students["Emma"] = 95; // Modify Emma's marks
print_r($students);
?>

3. Loop Through an Associative Array


Write a PHP program to iterate through an associative array using foreach and print
all key-value pairs.
<?php
$students = ["John" => 85, "Emma" => 90, "Mike" => 78];
foreach ($students as $name => $marks) {
echo "$name scored $marks marks.<br>";
}
?>
4. Count Elements in an Associative Array
Write a PHP program to count the number of elements in an associative array using
count().
<?php
$students = ["John" => 85, "Emma" => 90, "Mike" => 78];

echo "Total students: " . count($students);


?>
5. Check if a Key Exists in an Associative Array
Write a PHP script to check if a specific key exists in an associative array using
array_key_exists().
<?php
$students = ["John" => 85, "Emma" => 90, "Mike" => 78];

if (array_key_exists("Emma", $students)) {
echo "Emma is in the students list.";
} else {
echo "Emma is not found.";
}
?>

6. Remove a Key from an Associative Array


Write a PHP script to remove a specific key from an associative array and re-index
it.
<?php
$students = ["John" => 85, "Emma" => 90, "Mike" => 78];
unset($students["Mike"]); // Remove Mike
print_r($students);?>
7. Sorting an Associative Array
Write a PHP script to sort an associative array:

By keys in ascending order.


<?php
$students = ["John" => 85, "Emma" => 90, "Mike" => 78];
ksort($students); // Sort by keys
print_r($students);
?>
By values in ascending order.
<?php
$students = ["John" => 85, "Emma" => 90, "Mike" => 78];
asort($students); // Sort by values
print_r($students);
?>
8. Merge Two Associative Arrays
Write a PHP script to merge two associative arrays into one.
<?php
$students1 = ["John" => 85, "Emma" => 90];
$students2 = ["Mike" => 78, "Sophia" => 92];
$merged = array_merge($students1, $students2);
print_r($merged);
?>

9. Search for a Value in an Associative Array


Write a PHP script to check if a specific value exists in an associative array.
<?php
$students = ["John" => 85, "Emma" => 90, "Mike" => 78];
if (in_array(90, $students)) {
echo "A student has 90 marks.";
} else {
echo "No student has 90 marks.";
}
?>

10. Reverse an Associative Array


Write a PHP script to reverse the order of elements in an associative array.
<?php
$students = ["John" => 85, "Emma" => 90, "Mike" => 78];
$reversed = array_reverse($students, true);
print_r($reversed);
?>

You might also like