Assignment : Array Part -1
Assignment 1:
Create and Manipulate an Indexed Array
Task:
Create an indexed array of five different fruits. Display the list of fruits, then replace one of the fruits with another fruit and display the updated list.
Code:
// Step 1: Create an indexed array with five
different fruits $fruits = ["Apple",
"Banana", "Cherry", "Mango",
"Orange"]; // Step 2: Display the list of fruits echo "Original list of fruits:\n"; foreach ($fruits as $fruit) { echo $fruit . "\n"; } // Step 3: Replace one of the fruits (e.g.,
Banana) with another fruit (e.g., Pineapple) $fruits[1] = "Pineapple"; // Step 4: Display the updated list of fruits echo "\nUpdated list of fruits:\n"; foreach ($fruits as $fruit) { echo $fruit . "\n"; } ?>
|
Explanation:
We create an indexed array $fruits with five elements.
We use a foreach loop to display each fruit.
We replace the second fruit (Banana) with Pineapple by assigning a new value to the index 1.
We display the updated list using another foreach loop.
Assignment 2:
Create and Access an Associative Array
Task:
Create an associative array that stores the names of three students and their corresponding grades. Display each student's name and grade.
Code:
// Step 1: Create an associative array with
students' names and grades $grades = [ "Alice" => 90, "Bob" => 85, "Charlie" => 88 ]; // Step 2: Display each student's name and
grade echo "Students and their
grades:\n"; foreach ($grades as $name => $grade) { echo "$name:
$grade\n"; } ?>
|
Explanation:
We create an associative array $grades where the keys are student names and the values are their grades.
We use a foreach loop to display each student's name and grade.
Assignment 3:
Use Built-in Functions with Arrays
Task:
Create an indexed array of colors. Add a new color to the array, remove the last color, and then display the keys and values of the array.
Code:
<?php // Step 1: Create an indexed array with some
colors $colors = ["Red",
"Green", "Blue", "Yellow"]; // Step 2: Add a new color to the array using
array_push() array_push($colors, "Purple"); // Step 3: Remove the last color from the
array using array_pop() array_pop($colors); // Step 4: Display the keys and values of the
array echo "Array keys:\n"; $keys = array_keys($colors); foreach ($keys as $key) { echo $key . "\n"; } echo "\nArray values:\n"; $values = array_values($colors); foreach ($values as $value) { echo $value . "\n"; } ?>
|
Explanation:
We start by creating an indexed array $colors with four colors.
We use array_push() to add a new color (Purple) to the end of the array.
We use array_pop() to remove the last color from the array.
We display the keys and values of the array separately using array_keys() and array_values() functions.
Assignment 4: Create a Multi-dimensional Array
Task:
Create a multi-dimensional array to store information about three cars (make, model, and year). Display the information for each car.
Code:
<?php // Step 1: Create a multi-dimensional array
to store cars' information $cars = [ ["Make" =>
"Toyota", "Model" => "Corolla",
"Year" => 2020], ["Make" =>
"Honda", "Model" => "Civic",
"Year" => 2019], ["Make" =>
"Ford", "Model" => "Focus", "Year"
=> 2018] ]; // Step 2: Display the information for each
car echo "Cars information:\n"; foreach ($cars as $car) { echo "Make: " .
$car["Make"] . ", Model: " . $car["Model"] .
", Year: " . $car["Year"] . "\n"; } ?>
|
Explanation:
We create a multi-dimensional array $cars where each element is an associative array containing the make, model, and year of a car.
We use a foreach loop to iterate through the array and display each car's information.
Assignment 5: Sort an Associative Array
Task:
Create an associative array of five countries and their populations. Sort the array by population (in ascending order) and display the sorted list.
Code:
<?php // Step 1: Create an associative array with
countries and their populations $populations = [ "China" =>
1400000000, "India" =>
1350000000, "USA" =>
331000000, "Indonesia" =>
273000000, "Pakistan" =>
220000000 ]; // Step 2: Sort the array by population in
ascending order asort($populations); // Step 3: Display the sorted list echo "Countries sorted by
population:\n"; foreach ($populations as $country =>
$population) { echo "$country:
$population\n"; } ?>
|
Explanation:
We create an associative array $populations with countries as keys and their populations as values.
We use asort() to sort the array by population in ascending order, keeping the association between keys and values intact.
We display the sorted list using a foreach loop.
Create an indexed array of three different Cars. Display the details .
Code:
<!DOCTYPE html>
<html>
<body>
<pre>
<?php
$cars = array("Volvo", "BMW", "Toyota");
var_dump($cars);
?>
</pre>
</body>
</html>
In PHP, the function var_dump()
is used to display structured information about a variable, including its data type and value. It is particularly useful for debugging because it shows more details than just printing the value.
When you call var_dump($cars);
, it will display detailed information about the $cars
array, including the array's size (number of elements), each key, and its corresponding value.
Explanation of Each Section:
1. <!DOCTYPE html>
This declaration defines the document type and version of HTML. It is essential in modern web pages and tells the browser to render the page in standards mode. Here, it indicates that the document is written in HTML5.
2. <html>...</html>
This is the root element of the HTML document. All other elements of the web page are contained within this <html> tag.
3. <body>...</body>
The <body> tag defines the main content of the HTML document that will be displayed to the user. Everything inside the <body> tag appears on the web page.
4. <pre>...</pre>
The <pre> tag is used to display text exactly as it is written, preserving spaces and line breaks. It stands for "preformatted text." In this case, it is used to ensure that the output of var_dump() (which has a structured format) is displayed properly, preserving the indentation and layout.
5. <?php ... ?>
This section starts and ends the PHP code inside the HTML document. Anything inside <?php ... ?> is processed by the PHP interpreter on the server, while the rest of the document is plain HTML.
6. $cars = array("Volvo", "BMW", "Toyota");
Here, an array called $cars is created using the array() function. It holds three elements:
"Volvo"
"BMW"
"Toyota"
7. var_dump($cars);
The var_dump() function is used here to display detailed information about the $cars array. It will show:
The data type of the variable (in this case, an array).
The number of elements in the array.
Each element's data type and value.
The output of this var_dump() will be:
array(3) {
[0]=> string(5) "Volvo"
[1]=> string(3) "BMW"
[2]=> string(6) "Toyota"
}
8. Output Explanation:
array(3): This indicates that $cars is an array with 3 elements.
[0] => string(5) "Volvo": This shows that the first element has an index of 0, its type is string, and the value is "Volvo". The 5 inside string(5) indicates that the string has 5 characters.
[1] => string(3) "BMW": This shows that the second element has an index of 1, it is also a string, and its value is "BMW". The string is 3 characters long.
[2] => string(6) "Toyota": Similarly, the third element has an index of 2, and it is a string with the value "Toyota". The string is 6 characters long.