StartVader StartVader - 6 months ago 18
PHP Question

Replace numbers with text

I get data for a user

table
in
Moodle
:

$data[] = $view-look;
$data[] = $view->tyres;
$data[] = $view->insidelook;
$data[] = $review->motor;


And the data is for the
status
in each column. Statuses are the same from
bad (1)
,
good(2)
,
very good (3)
to
superb(4)
.

Right now only numbers are being shown. How to show, instead of the numbers, the bad, good etc. for each column?

Answer

Easiest way

    $data = [1,1,1,2,4,2,3,3,1,3,3,2];

    $mapping = [1 => 'bad', 2 => 'good', 3 => 'very good', 4 => 'superb'];

    foreach ($data as $key => $value) {
        $data[$key] = $mapping[$value]; 
    }

You will get the same array $data with the text content instead of numbers.