📖 Scenario: You have a list of fruits with their prices. You want to organize this list in different ways to find the cheapest or most expensive fruits easily.
🎯 Goal: Learn how to use PHP array sort functions to sort an associative array by keys and values in ascending and descending order.
📋 What You'll Learn
Create an associative array with fruit names as keys and prices as values.
Create a variable to hold the sorted array.
Use PHP array sort functions to sort the array by values ascending and descending.
Use PHP array sort functions to sort the array by keys ascending and descending.
Print the sorted arrays to see the results.
💡 Why This Matters
🌍 Real World
Sorting arrays is useful when you want to organize data like product prices, names, or scores to find the best or worst quickly.
💼 Career
Knowing how to sort arrays is a basic skill for web developers and programmers who handle data and need to display it in a user-friendly order.
Progress0 / 4 steps
1
Create the fruits array
Create an associative array called fruits with these exact entries: 'Apple' => 3, 'Banana' => 1, 'Cherry' => 2, 'Date' => 5, 'Elderberry' => 4.
PHP
Hint
Use square brackets [] to create the array and separate each fruit and price with commas.
2
Create a variable for sorted array
Create a variable called sortedFruits and set it equal to $fruits to prepare for sorting.
PHP
Hint
Assign the existing array $fruits to a new variable $sortedFruits so you can sort it without changing the original.
3
Sort the array by values ascending
Use the PHP function asort() on $sortedFruits to sort the array by values in ascending order.
PHP
Hint
Use asort() to sort the array by its values while keeping the keys.
4
Print the sorted array
Print the $sortedFruits array using print_r() to display the sorted fruits by price ascending.
PHP
Hint
Use print_r() to show the array structure and values clearly.
Practice
(1/5)
1. Which PHP function sorts an array and keeps the original keys intact?
easy
A. rsort()
B. sort()
C. ksort()
D. asort()
Solution
Step 1: Understand key preservation in sorting
asort() sorts values but keeps the original keys unchanged.
Step 2: Compare with other functions
sort() resets keys, rsort() sorts in reverse but resets keys, ksort() sorts by keys, not values.
Final Answer:
asort() -> Option D
Quick Check:
Keep keys while sorting values = asort() [OK]
Hint: Use asort() to sort values and keep keys unchanged [OK]
Common Mistakes:
Confusing sort() with asort() for key preservation
Thinking ksort() sorts by values
Assuming rsort() keeps keys
2. Which of the following is the correct syntax to sort an array in ascending order and reset keys in PHP?
easy
A. asort($array);
B. sort($array);
C. ksort($array);
D. arsort($array);
Solution
Step 1: Identify function for ascending sort with key reset
sort() sorts array values in ascending order and resets keys.
Step 2: Check other options
asort() keeps keys, ksort() sorts by keys, arsort() sorts descending.
Final Answer:
sort($array); -> Option B
Quick Check:
Ascending sort with reset keys = sort() [OK]
Hint: Use sort() to sort ascending and reset keys [OK]
Common Mistakes:
Using asort() when keys should reset
Confusing ksort() with value sort
Using arsort() for ascending order
3. What will be the output of the following PHP code?