Alex Rivera | Logout

Get first non-null value from a flat array

Asked 2012-06-22T15:14:07.000
21

If I have an array:

[null, 'a', 'b', 'c']

I want to get the first non-null value from the array, in this case "a". How could I go about doing that nice and easily?

Edit
Report

1 Answer

3
function getFirstNotNull($array) {
    foreach($array as $val) {
         if(!is_null($val) || !$val) return $val;
    }
}
answered 2012-06-22T15:16:23.170

Your Answer