0
0
PHPprogramming~15 mins

Array unique and flip in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Array unique and flip
📖 Scenario: You have a list of fruits with some duplicates. You want to find the unique fruits and then flip the list so that fruits become keys and their positions become values.
🎯 Goal: Create a PHP program that removes duplicate fruits from an array and then flips the array so that fruit names become keys and their indexes become values.
📋 What You'll Learn
Create an array called fruits with the exact values: 'apple', 'banana', 'apple', 'orange', 'banana'
Create a variable called uniqueFruits that holds the unique values from fruits
Create a variable called flippedFruits that flips the keys and values of uniqueFruits
Print the flippedFruits array
💡 Why This Matters
🌍 Real World
Removing duplicates and flipping arrays is useful when you want to quickly find the position of unique items or create lookup tables.
💼 Career
These skills are important for data cleaning, preparing data for search, and optimizing lookups in PHP web applications.
Progress0 / 4 steps
1
Create the initial array
Create an array called fruits with these exact values in order: 'apple', 'banana', 'apple', 'orange', 'banana'
PHP
Need a hint?

Use square brackets [] to create the array and separate values with commas.

2
Remove duplicates from the array
Create a variable called uniqueFruits that holds the unique values from the fruits array using the array_unique() function
PHP
Need a hint?

Use array_unique($fruits) to get unique values.

3
Flip the unique array
Create a variable called flippedFruits that flips the keys and values of the uniqueFruits array using the array_flip() function
PHP
Need a hint?

Use array_flip($uniqueFruits) to swap keys and values.

4
Print the flipped array
Print the flippedFruits array using print_r() to display the result
PHP
Need a hint?

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