检查对象数组是否不为空
Check if array of object is not empty
我是 Codeigniter 的新手,面对这个问题我非常绝望。所以,我只是用 CI 做了一个简单的应用程序。当我想检查对象数组(在本例中为 $kunj->DIAG 数组)时,我收到一条错误通知。
这是我的一些观点:
查看:
<table class="table table-striped table-bordered table-condensed" id="tablescroll" role="grid">
<thead>
<tr class="tablesorter-headerRow" role="row">
<th>No.</th>
<th>Nama</th>
<?php if(!empty($kunj->DIAG)){ echo "<th>ICD</th>"; } ?>
</tr>
</thead>
<tbody aria-live="polite" aria-relevant="all">
<?php if(!empty($kunj)) print_r(count($kunj));
if(!empty($kunj)){
$no = 0;
foreach ($kunj as $i): ?>
<tr align='center'>
<td><?php $no++; echo $no; ?></td>
<td><?php echo $i->NAMA; ?></td>
<?php if(!empty($i->DIAG)){ echo "<td>".$i->DIAG."</td>"; } ?>
</tr>
<?php endforeach ?>
<?php } ?>
</tbody>
</table>
控制器:
function kunjunganpx(){
$this->load->model('simrs/diagnosa_model');
$cari = array(
'icd' => $this->input->post('icd'),
'asr' => $this->input->post('asuransi'),
'tgl_strt' => $this->input->post('tgl_strt'),
'tgl_end' => $this->input->post('tgl_end'),
'stat' => $this->input->post('px_stat'),
'urut' => $this->input->post('urut'),
'opsi' => $this->input->post('opsi'));
$data['kunj'] = $this->diagnosa_model->get_kunjungan_list($cari);
$this->load->view('kunjunganpx_view',$data);
}
这是我收到的通知:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: simrs/kunjunganpx_view.php
通过谷歌搜索一些网站,我意识到对象数组不能用 empty()
检查。但我非常想知道如何解决这个问题,因为我在对象中生成查询结果。
我只想知道当 $kunj->DIAG 中没有值时如何 'hide' ICD 硬印。如果变量有一些值,则显示它。提前致谢。
更新
应会员的要求,我在此处附上 var_dump($data);
结果:
array(1) { ["kunj"]=> array(2)
{ [0]=> object(stdClass)#21 (20)
{ ["NAMA"]=> string(13) "SUPRIYANTO BP"
["DIAG"]=> string(5) "D48.0" }
[1]=> object(stdClass)#22 (20)
{ ["NAMA"]=> string(13) "SUPRIYANTO BP"
["DIAG"]=> string(5) "C44.9" }
}
}
if($kunj->DIAG != ''){ echo "<th>ICD</th>"; } ?>
我认为这会解决它,因为有时
正如我所看到或理解的那样,您没有得到 null 值,但是在那些变量中得到 empty。
可以试试这个:
if (is_object($kunj) && !empty($kunj->DIAG)) {
// do your stuff
}
OK ... 基于 $kunj=array()
这就是我想出的事实。
empty()
是行不通的,因为对象有或没有 属性。如果它有 属性,那么你想解析额外的列。
您需要测试对象中是否存在 属性 DIAG。
if (property_exists($kunj[0], "DIAG"))
之后你可以测试 $kunj
是否为空,就像你正在做的那样。在 foreach 循环中,您将再次必须在 属性 上进行测试,然后再解析额外的 table 单元格。如果 属性 存在,则无需测试是否为空。
我也改变了你的一些逻辑。这一切的结果是:
<table class="table table-striped table-bordered table-condensed" id="tablescroll" role="grid">
<thead>
<tr class="tablesorter-headerRow" role="row">
<th>No.</th>
<th>Nama</th>
<?php
if (property_exists($kunj[0], "DIAG")) {
echo "<th>ICD</th>";
}
?>
</tr>
</thead>
<tbody aria-live="polite" aria-relevant="all">
<?php if(!empty($kunj)){ foreach ($kunj as $k => $i): ?>
<tr align='center'>
<td><?php echo ++$k; ?></td>
<td><?php echo $i -> NAMA; ?></td>
<?php
if (property_exists($kunj[0], "DIAG")) {
echo "<td>{$i->DIAG}</td>";
}
?>
</tr>
<?php endforeach; } ?>
</tbody>
</table>
我遇到了这个问题,is_null 是解决方案
试试这个:
if(!is_null($kunj->DIAG)){echo "<th>ICD</th>";}
我是 Codeigniter 的新手,面对这个问题我非常绝望。所以,我只是用 CI 做了一个简单的应用程序。当我想检查对象数组(在本例中为 $kunj->DIAG 数组)时,我收到一条错误通知。 这是我的一些观点:
查看:
<table class="table table-striped table-bordered table-condensed" id="tablescroll" role="grid">
<thead>
<tr class="tablesorter-headerRow" role="row">
<th>No.</th>
<th>Nama</th>
<?php if(!empty($kunj->DIAG)){ echo "<th>ICD</th>"; } ?>
</tr>
</thead>
<tbody aria-live="polite" aria-relevant="all">
<?php if(!empty($kunj)) print_r(count($kunj));
if(!empty($kunj)){
$no = 0;
foreach ($kunj as $i): ?>
<tr align='center'>
<td><?php $no++; echo $no; ?></td>
<td><?php echo $i->NAMA; ?></td>
<?php if(!empty($i->DIAG)){ echo "<td>".$i->DIAG."</td>"; } ?>
</tr>
<?php endforeach ?>
<?php } ?>
</tbody>
</table>
控制器:
function kunjunganpx(){
$this->load->model('simrs/diagnosa_model');
$cari = array(
'icd' => $this->input->post('icd'),
'asr' => $this->input->post('asuransi'),
'tgl_strt' => $this->input->post('tgl_strt'),
'tgl_end' => $this->input->post('tgl_end'),
'stat' => $this->input->post('px_stat'),
'urut' => $this->input->post('urut'),
'opsi' => $this->input->post('opsi'));
$data['kunj'] = $this->diagnosa_model->get_kunjungan_list($cari);
$this->load->view('kunjunganpx_view',$data);
}
这是我收到的通知:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: simrs/kunjunganpx_view.php
通过谷歌搜索一些网站,我意识到对象数组不能用 empty()
检查。但我非常想知道如何解决这个问题,因为我在对象中生成查询结果。
我只想知道当 $kunj->DIAG 中没有值时如何 'hide' ICD 硬印。如果变量有一些值,则显示它。提前致谢。
更新
应会员的要求,我在此处附上 var_dump($data);
结果:
array(1) { ["kunj"]=> array(2)
{ [0]=> object(stdClass)#21 (20)
{ ["NAMA"]=> string(13) "SUPRIYANTO BP"
["DIAG"]=> string(5) "D48.0" }
[1]=> object(stdClass)#22 (20)
{ ["NAMA"]=> string(13) "SUPRIYANTO BP"
["DIAG"]=> string(5) "C44.9" }
}
}
if($kunj->DIAG != ''){ echo "<th>ICD</th>"; } ?>
我认为这会解决它,因为有时
正如我所看到或理解的那样,您没有得到 null 值,但是在那些变量中得到 empty。
可以试试这个:
if (is_object($kunj) && !empty($kunj->DIAG)) {
// do your stuff
}
OK ... 基于 $kunj=array()
这就是我想出的事实。
empty()
是行不通的,因为对象有或没有 属性。如果它有 属性,那么你想解析额外的列。
您需要测试对象中是否存在 属性 DIAG。
if (property_exists($kunj[0], "DIAG"))
之后你可以测试 $kunj
是否为空,就像你正在做的那样。在 foreach 循环中,您将再次必须在 属性 上进行测试,然后再解析额外的 table 单元格。如果 属性 存在,则无需测试是否为空。
我也改变了你的一些逻辑。这一切的结果是:
<table class="table table-striped table-bordered table-condensed" id="tablescroll" role="grid">
<thead>
<tr class="tablesorter-headerRow" role="row">
<th>No.</th>
<th>Nama</th>
<?php
if (property_exists($kunj[0], "DIAG")) {
echo "<th>ICD</th>";
}
?>
</tr>
</thead>
<tbody aria-live="polite" aria-relevant="all">
<?php if(!empty($kunj)){ foreach ($kunj as $k => $i): ?>
<tr align='center'>
<td><?php echo ++$k; ?></td>
<td><?php echo $i -> NAMA; ?></td>
<?php
if (property_exists($kunj[0], "DIAG")) {
echo "<td>{$i->DIAG}</td>";
}
?>
</tr>
<?php endforeach; } ?>
</tbody>
</table>
我遇到了这个问题,is_null 是解决方案
试试这个:
if(!is_null($kunj->DIAG)){echo "<th>ICD</th>";}