-2

可能重复:
删除空数组元素

我有一个这样的数组

Array ( [81] => 5.00 [78] => 4.67 [13] => 4.67 [65] => 4.60 [91] => 4.50 [4] => 4.40 [7] => 4.25 [11] => 4.20 [47] => 4.20 [21] => 4.20 [66] => 4.17 [48] => 4.00 [37] => 4.00 [69] => 4.00 [49] => 4.00 [38] => 4.00 [54] => 4.00 [62] => 4.00 [63] => 4.00 [64] => 4.00 [61] => 4.00 [60] => 4.00 [52] => 4.00 [59] => 4.00 [80] => 4.00 [50] => 4.00 [34] => 4.00 [16] => 4.00 [18] => 4.00 [9] => 4.00 [5] => 4.00 [3] => 4.00 [19] => 4.00 [20] => 4.00 [25] => 4.00 [42] => 3.83 [73] => 3.83 [28] => 3.83 [58] => 3.83 [15] => 3.83 [45] => 3.80 [10] => 3.80 [67] => 3.75 [17] => 3.75 [2] => 3.75 [27] => 3.75 [32] => 3.75 [51] => 3.75 [1] => 3.75 [31] => 3.71 [53] => 3.70 [72] => 3.70 [26] => 3.67 [82] => 3.67 [12] => 3.67 [71] => 3.67 [77] =>  [70] =>  [41] => 3.67 [8] => 3.60 [22] => 3.60 [57] => 3.60 [39] => 3.60 [83] => 3.60 [76] => 3.50 [74] => 3.50 [79] => 3.50 [43] => 3.50 [35] => 3.50 [14] => 3.50 [6] => 3.50 [44] => 3.50 [33] => 3.50 [89] => 3.33 [68] => 3.30 [29] => 3.25 [40] => 3.20 [24] => 3.17 [30] => 3.13 [56] => 3.00 [36] => 3.00 [23] => 3.00 [55] => 3.00 [46] => 2.67 [90] =>  ) 

现在我想要的是从这个数组中删除空白值索引。提前致谢。

4

4 回答 4

2
foreach($array as $key => $val)
{
    if(empty($val)) unset($array[$key]);
}
print_r($array);

这将取消设置数组中没有键的任何部分

于 2012-11-26T07:09:54.827 回答
2

您可以遍历它,取消设置任何空值。

foreach ($array as $key => $value) {
    if (empty($value)) {
        unset($array[$key]);
    }
}
于 2012-11-26T07:10:45.263 回答
2
function is_not_blank($value) {
    return $value !== false && !is_null($value) && trim($value) !== "";
}

$myArray = array_filter($myArray, "is_not_blank");
于 2012-11-26T07:12:04.057 回答
1

您可以使用array_filter

function not_empty($a)
{
    return !empty($a) && !is_null($a);
}

$array = array_filter($array, 'not_empty');
于 2012-11-26T07:15:16.540 回答