<?
php
// 1. Creating an Indexed Array
$fruits = array("Apple", "Banana", "Cherry");
// 2. Adding an Element to the Array
$fruits[] = "Date"; // Adds "Date" to the end of the array
// 3. Creating an Associative Array
$person = array(
"first_name" => "John",
"last_name" => "Doe",
"age" => 30
);
// 4. Adding an Element to the Associative Array
$person["email"] = "[Link]@[Link]";
// 5. Iterating Through the Indexed Array
echo "Fruits in the array:\n";
foreach ($fruits as $fruit) {
echo $fruit . "\n";
}
// 6. Iterating Through the Associative Array
echo "\nPerson Details:\n";
foreach ($person as $key => $value) {
echo ucfirst($key) . ": " . $value . "\n";
}
// 7. Array Functions: Sorting the Indexed Array
sort($fruits);
echo "\nSorted Fruits:\n";
foreach ($fruits as $fruit) {
echo $fruit . "\n";
}
// 8. Array Functions: Getting the Number of Elements in an Array
$numberOfFruits = count($fruits);
echo "\nNumber of fruits in the array: " . $numberOfFruits . "\n";
?>