删除空白或空数组php CI

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了删除空白或空数组php CI相关的知识,希望对你有一定的参考价值。

如何删除null或空数组?谢谢

Image example

我的代码

$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的主要内容,如果未能解决你的问题,请参考以下文章

在此查询中,我需要添加啥使其永远不会显示空白或空值?

如何为第一个表的空白或空值应用左连接?

ZipArchive php 获取损坏或空的 zip

php 在Yoast SEO中更改或删除OpenGraph输出的代码片段。此代码中有多个代码段。

在php中,用CI框架开发程序,有两个表相连接,可以同时删除两个表相关联的数据吗?怎么写删除的代码?

通过ZipArchive php获取损坏或空拉链