0
0
PHPprogramming~15 mins

Array access and modification in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Array access and modification
📖 Scenario: You are managing a small fruit store inventory. You have a list of fruits and their quantities. You want to update the quantities as fruits are sold or restocked.
🎯 Goal: Learn how to access and modify values in an array in PHP by updating fruit quantities.
📋 What You'll Learn
Create an associative array with fruit names as keys and quantities as values
Create a variable to hold the quantity to add or subtract
Modify the quantity of a specific fruit in the array
Print the updated array
💡 Why This Matters
🌍 Real World
Managing inventory in a store requires updating quantities as items are sold or restocked.
💼 Career
Knowing how to access and modify arrays is essential for backend development and data management.
Progress0 / 4 steps
1
Create the initial fruit inventory array
Create an associative array called fruits with these exact entries: 'apple' => 10, 'banana' => 5, 'orange' => 8.
PHP
Need a hint?

Use square brackets [] to create an associative array with keys and values.

2
Create a variable for quantity change
Create a variable called change and set it to -3 to represent selling 3 fruits.
PHP
Need a hint?

Use the = sign to assign the value -3 to the variable change.

3
Modify the quantity of bananas
Update the quantity of banana in the fruits array by adding the value of change to it.
PHP
Need a hint?

Use $fruits['banana'] += $change; to add change to the current banana quantity.

4
Print the updated fruits array
Use print_r to display the updated fruits array.
PHP
Need a hint?

Use print_r($fruits); to print the array in a readable format.