0

所以我有一个有很多选项的大数组,全部用于汽车,所以里程和声音等等现在可以说我的客户不提供里程,在我将这个数组发送到我的函数之前,我想确保每个元素都不为空,那些没有被放置在一个新的数组中,将被发送用于 sql 插入

这是我的数组:

Array ( [year] => select [make] => Buick [model] => [engine] => [mileage] => [price] => [vin] => [att1] => [att2] => [att3] => [att4] => [bodystyle] => [fuel] => [hp] => [cyl] => Select [enginesize] => L [transmission] => [shifts] => [od] => [sound] => [gps] => [sound_system] => [sradio] => [tachometer] => [clock] => [trip] => [eweather] => [digitalboard] => [drive] => [fxf] => [cruisecontrol] => [tiltsteering] => [ac] => [removabletop] => [keyless] => [airbags] => [alloy] => [trunkantitrap] => [ewindows] => [emirrors] => [eseat] => [elocks] => [antitheft] => [ledheadlights] => )

那么我如何循环遍历它,如果它不是空的,则将它添加到一个新数组中,同时也是一个关联的集合名称和值

对不起,我有点困惑

4

2 回答 2

4

您可以使用以下命令从数组中删除空值array_filter

$arr = ['foo' => null, 'bar' => 'not null'];
$filtered = array_filter($arr); // contains just "bar" => "not null"

但是,请注意,这将删除所有比较等于 boolean 的值false在其他值中,这包括空字符串、字符串"0"和整数0。如果您不想删除这些值,则必须向array_filter. 例如,仅删除null值:

$arr = ['foo' => null, 'bar' => '0'];
$filtered = array_filter($arr, function($o) { return $o !== null; });
于 2013-08-07T09:23:27.793 回答
1

试试喜欢

$new_arr = array();
foreach($my_arr as $key => $value) {
    if($value != '' && $value != NULL )
         $new_arr[$key] = $value;
} 
于 2013-08-07T09:23:28.747 回答