-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.

5
  • What actually is "blank"? Provide var_dump($arr); instead Commented Nov 26, 2012 at 7:08
  • stackoverflow.com/questions/7225070/… Commented Nov 26, 2012 at 7:09
  • this might work - $arr = array_filter($arr); Commented Nov 26, 2012 at 7:09
  • @NiravRanpara - the selected answer, i think is broken Commented Nov 26, 2012 at 7:10
  • @Nirav Ranpara: and what would you recommend to use as a needle? Commented Nov 26, 2012 at 7:10

4 Answers 4

2

You can iterate through it, unsetting any empty values.

foreach ($array as $key => $value) {
    if (empty($value)) {
        unset($array[$key]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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

11 Comments

Stupid mistake, good catch!
Try again, $key isn't key but a value
@Vulcan: CDD - comments driven development
How about now? (This is ebarassing!)
@PRPGFerret: please look carefully through your answer modifications
|
2
function is_not_blank($value) {
    return $value !== false && !is_null($value) && trim($value) !== "";
}

$myArray = array_filter($myArray, "is_not_blank");

2 Comments

How about nulls and ' '?
@zerkms: You are right, I haven't included those - I have now modified my answer.
1

You can use array_filter

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

$array = array_filter($array, 'not_empty');

6 Comments

It's sooooooo wrong. isset($a) would always return true (in case if $a is, say, empty string).
@cryptic: empty() won't work for ' ' (which may be considered as a "blank" by OP)
@zerkms Well, it would return true for null values :)
@Jack: for null - yes. For ' ' - no. From my perspective the space char is a perfect candidate for "blank" ;-) (for newbie OP)
@zerkms Yeah, at this point we're all speculating what empty might entail =p
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.