Skip to content
PHP Logo

How can PHP functions like explode, array_merge, and array_unique be utilized to manipulate and sort data retrieved from a database?

When retrieving data from a database, it may be necessary to manipulate and sort the data before displaying it. PHP functions like explode, array_merge, and array_unique can be used to achieve this. For example, explode can be used to split a string into an array, array_merge can be used to combine multiple arrays into one, and array_unique can be used to remove duplicate values from an array.

php
// Retrieve data from the database
$data = "apple,banana,orange,apple,grape";
$items = explode(",", $data);

// Remove duplicates
$unique_items = array_unique($items);

// Sort the array
sort($unique_items);

// Display the sorted and unique items
foreach ($unique_items as $item) {
    echo $item . "<br>";
}