"Welcome to our PHP 101 blog, where we demystify the world of web development. "

Wednesday, September 4, 2024

Assignment : Array part 1

 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:


<?php

// 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:


<?php

// 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.


No comments:

Post a Comment

Pages

SoraTemplates

Best Free and Premium Blogger Templates Provider.

Buy This Template