You are here: Re: Slick way to check if array contains empty elements? « PHP Programming Language « IT news, forums, messages
Re: Slick way to check if array contains empty elements?

Posted by ZeldorBlat on 02/12/06 00:55

Dave Mennenoh wrote:
> ...good one-liner...
>
> This is what I came up with:
>
> function isEmpty($array){
> return (count(array_filter($array)) == 0) ? 1 : 0;
> }
>
> returns 1 if all elements are "" otherwise it returns 0.
>
>
> --
> Dave -
> www.blurredistinction.com
> www.macromedia.com/support/forums/team_macromedia/

It really depends on what the OP means by "empty." The code above will
return 1 if the array only contains elements that evaluate to false.
Examples of things that evaluate to false: false, "", 0, null. So,
given the array (0, 0, 0) that function will return true.

If your definition of "empty" is really the empty string, you can make
a small change to Dave's function as such:

function isEmpty($array) {
$my_not_empty = create_function('$v', 'return strlen($v) > 0;');
return (count(array_filter($array, $my_not_empty)) == 0) ? 1 : 0;
}

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация