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.
// 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>";
}
Related questions
-
float property image positioning PHP
-
security risks file inclusion vulnerability open_basedir restriction
-
batch file PHP script variables