需要从数组中删除[null]

时间:2017-04-04 10:20:32

标签: php arrays

Array ( [0] => [null] [1] => [null] [2] => [null] [3] => [null] [4] => [null] [5] => [null] [6] => [null] [7] => [null] [8] => [null] [9] => [null] [10] => [{"SL":"2","S":"34","M":"4","L":"5","F":"5"}] [11] => [{"SL":"5","S":"3","M":"4","L":"5","F":"0"}] )

需要输出:

Array([0]=>[{"SL":"2","S":"34","M":"4","L":"5","F":"5"}] [1]=>[{"SL":"5","S":"3","M":"4","L":"5","F":"0"}])

5 个答案:

答案 0 :(得分:0)

您可以使用数组过滤器函数array_filter()来排除空值。

 array_filter($Array, function($val) { return $val !== null; })

答案 1 :(得分:0)

$arr = array('abc',NULL,'rre',null,null,null,'dfg','fggh');

$emptyRemoved = remove_empty($arr);

function remove_empty($array) {
  return array_filter($array, '_remove_empty_internal');
}

function _remove_empty_internal($value) {
  return !empty($value) || $value === 0;
}

答案 2 :(得分:0)

仅限过滤器" null"您可以在没有回调的情况下使用array_filter的值。要创建新键(0..n),请使用array_values(因为array_filter保留键) 结果代码:

$result = array_values(array_filter($yourArray));

答案 3 :(得分:0)

试试这个钩子:

array_filter($arr, function($var){return !is_null($var);} );

答案 4 :(得分:0)

请试试这个:

_template