How can PHP be utilized to replace database values with strings from an array for sorting and filtering purposes?

When sorting or filtering data from a database, it may be necessary to replace certain values with more user-friendly strings. This can be achieved by creating an associative array where the keys are the database values and the corresponding values are the strings to be displayed. Then, when fetching data from the database, we can use PHP to replace the values with the strings before displaying them to the user.

// Example array mapping database values to display strings
$valueMapping = [
    1 => 'Active',
    2 => 'Inactive',
    3 => 'Pending',
];

// Sample database query result
$dataFromDatabase = [
    ['id' => 1, 'status' => 1],
    ['id' => 2, 'status' => 2],
    ['id' => 3, 'status' => 3],
];

// Replace database values with strings for display
foreach ($dataFromDatabase as $row) {
    $row['status'] = $valueMapping[$row['status']];
    echo "ID: {$row['id']} | Status: {$row['status']}<br>";
}