Give Name as number in Array foreach

0

I have this foreach below that lists results of a Select, I would like to name it according to the number, as I did below in the IF, but it still did not work ..

foreach($result as $row)  
    {  

     $segurado[]           = $row['SEGURADO'];     
     $data[]           = $row['DATA'];     
     $status_seguro1       = $row['STATUS_SEGURO'];

        if($status_seguro1 == '1') {$status_seguro[] = 'Vistoria';}
        if($status_seguro1 == '2') {$status_seguro[] = 'Primeira Parcela';}
        if($status_seguro1 == '3') {$status_seguro[] = 'Apólice';}
        if($status_seguro1 == '4') {$status_seguro[] = 'Cancelado';}

    }
    
asked by anonymous 01.12.2017 / 20:03

2 answers

1

As in this ifs you are just making simple assignments, you can change this approach by an array, so just access the appropriate index and assign the respective value.

$status = array(1 => 'Vistoria', 2 => 'Primeira Parcela', 3 => 'Apólice', 4 => 'Cancelado');
foreach($result as $row){  
    $segurado[] = $row['SEGURADO'];     
    $data[] = $row['DATA'];     
    $status_seguro[] = isset($status[$row['STATUS_SEGURO']]) ? $status[$row['STATUS_SEGURO']] : 'Inválido';
}
    
01.12.2017 / 20:26
-1

Why not create a table with the status? So in the future if you need to change a status or even include a new one, just include it in your DB. Without using the database the best option is the one of the rray.

    
02.12.2017 / 17:40