I pass $data["results"] array from my controller to my view and I want to echo NAMEs of those array elements which equals 1.
For example, if {$first ==1, $second == 0, $third == 0} I want to display "first".
Could you please check my code below and help me to find my mistake.
foreach($results as $row){
$first= $row->first;
$second= $row->second;
$third= $row->third;
if ($first == 1) {$digits['first'] = $first;}
if ($second == 1) {$digits['second'] = $second;}
if ($third == 1) {$digits['third'] = $third;}
print_r($digits); // Displays 'Array ( [first] => 1 )' instead of 'first'
}
Update:
I generate html tables through the loop and display them with TCPDF.
Updated code below normally displays 'first' if {$first ==1, $second == 0, $third == 0} for the first table.
For the second table if {$first == 0, $second == 0, $third == 1} it should display 'third' but it displays 'first, third' because it adds the new value to previous one instead of replacing it.
$digits = array();
foreach($results as $row){
$first= $row->first;
$second= $row->second;
$third= $row->third;
if ($first == 1) { $digits[] = 'first'; }
if ($second == 1) { $digits[] = 'second'; }
if ($third == 1) { $digits[] = 'third'; }
$abc = implode(', ', $digits);
$tbl.=<<<EOD
<table>
<tr>
<td>
$abc
</td>
</tr>
</table>
<br><br>
EOD;
}