PHP displaying array element names -
PHP displaying array element names -
i pass $data["results"] array controller view , want echo names of array elements equals 1.
for example, if {$first ==1, $second == 0, $third == 0} want display "first".
could please check code below , help me find mistake.
foreach($results $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 loop , display them tcpdf.
updated code below displays 'first' if {$first ==1, $second == 0, $third == 0} first table.
for sec table if {$first == 0, $second == 0, $third == 1} should display 'third' displays 'first, third' because adds new value previous 1 instead of replacing it.
$digits = array(); foreach($results $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; }
the next code loop through array , add together 'first', 'second' or 'third' $digits array.
is trying achieve?
foreach($results $row){ $digits = array(); $first= $row->first; $second= $row->second; $third= $row->third; if ($first == 1) { $digits[] = 'first'; } if ($second == 1) { $digits[] = 'second'; } if ($third == 1) { $digits[] = 'third'; } print_r($digits); // echo implode(', ', $digits); }
php
Comments
Post a Comment