删除空白或空数组php CI
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了删除空白或空数组php CI相关的知识,希望对你有一定的参考价值。
如何删除null或空数组?谢谢
我的代码
$ar = array(
'id_cs' =>$row['AN'],
'nama' =>$row['AO'],
'tlp' =>$row['AP'],
'alamat_detail' =>$row['AQ'],
'id_provinsi' =>$prov,
'id_kota' =>$kot,
'id_kecamatan' =>$kec,
'id_kelurahan' =>$kelurahans,
);
foreach($ar as $key => $value)
if(empty($value)){
unset($ar[$key]);
}
echo '<pre>', print_r($ar);
输出图像。
我想要这样的输出
Array
(
[id_cs] => 16
[nama] => PT. xx
[tlp] => xx
[alamat_detail] => JL. MAYJEN SUTOYO
[id_provinsi] => 1
[id_kota] => 4
[id_kecamatan] => 32
[id_kelurahan] => 205
)
1
Array
(
[id_cs] => 16
[nama] => PT. xxxxx
[tlp] => xxxxx
[alamat_detail] => JL. MAYJEN SUTOYO
[id_provinsi] => 1
[id_kota] => 4
[id_kecamatan] => 32
[id_kelurahan] => 205
)
如何通过删除null或空数组来实现此目的。谢谢
答案
您可以这样操作:
if(empty($prov) && empty($kot) && empty($kec) && empty($kelurahans)){
$prov = 1;
$kot = 4;
$kec = 32;
$kelurahans = 205;
} //Of course you can divide this check in to a separate checks to get a separate changes on variables
$ar = array(
'id_cs' =>$row['AN'],
'nama' =>$row['AO'],
'tlp' =>$row['AP'],
'alamat_detail' =>$row['AQ'],
'id_provinsi' =>$prov,
'id_kota' =>$kot,
'id_kecamatan' =>$kec,
'id_kelurahan' =>$kelurahans,
);
foreach($ar as $key => $value)
if(empty($value)){
unset($ar[$key]);
}
}
echo '<pre>', print_r($ar);
您将获得所需的输出:
Array
(
[id_cs] => 16
[nama] => PT. xx
[tlp] => xx
[alamat_detail] => JL. MAYJEN SUTOYO
[id_provinsi] => 1
[id_kota] => 4
[id_kecamatan] => 32
[id_kelurahan] => 205
)
以上是关于删除空白或空数组php CI的主要内容,如果未能解决你的问题,请参考以下文章
php 在Yoast SEO中更改或删除OpenGraph输出的代码片段。此代码中有多个代码段。