-2

Possible Duplicate:
Remove empty array elements

I have an array like this

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] =>  ) 

Now what i want is to remove blank value indexes from this array. Thanks in advance.

Community
  • 1
  • 1
Ajay Kadyan
  • 1,081
  • 2
  • 13
  • 36

4 Answers4

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

This will unset any part of the array that does not have a key

zerkms
  • 249,484
  • 69
  • 436
  • 539
Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
2

You can iterate through it, unsetting any empty values.

foreach ($array as $key => $value) {
    if (empty($value)) {
        unset($array[$key]);
    }
}
FThompson
  • 28,352
  • 13
  • 60
  • 93
2
function is_not_blank($value) {
    return $value !== false && !is_null($value) && trim($value) !== "";
}

$myArray = array_filter($myArray, "is_not_blank");
vstm
  • 12,407
  • 1
  • 51
  • 47
1

You can use array_filter

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

$array = array_filter($array, 'not_empty');
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • 1
    It's sooooooo wrong. `isset($a)` would always return `true` (in case if `$a` is, say, empty string). – zerkms Nov 26 '12 at 07:16
  • @cryptic: `empty()` won't work for `' '` (which may be considered as a "blank" by OP) – zerkms Nov 26 '12 at 07:18
  • @zerkms Well, it would return `true` for `null` values :) – Ja͢ck Nov 26 '12 at 07:18
  • @Jack: for `null` - yes. For `' '` - no. From my perspective the space char is a perfect candidate for "blank" ;-) (for newbie OP) – zerkms Nov 26 '12 at 07:20
  • @zerkms Yeah, at this point we're all speculating what empty might entail =p – Ja͢ck Nov 26 '12 at 07:21
  • @Jack: yup, that's why I didn't even try to give my answer, though I was 1st here and had plenty of time for that ;-) – zerkms Nov 26 '12 at 07:22