473,748 Members | 2,328 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Slick way to check if array contains empty elements?

Hi,

Wondered if there was a good one-liner for what I want to do in PHP 4.
I have an array, with an arbitrary number of elements. I'd like to
know if all the elements in the array are empty. If at least one
element is non-empty, then the entire condition is false.

$my_arr = array();
$my_arr[0] = "";
$my_arr[1] = "";
$all_empty = something($my_a rr); // should contain the value TRUE

$my_arr = array();
$my_arr[0] = "";
$my_arr[1] = " hi ";
$my_arr[2] = "";
$all_empty = something($my_a rr); // should contain the value FALSE

Any slick ways to find this out? - Dave

Feb 11 '06 #1
3 34887
....good one-liner...

This is what I came up with:

function isEmpty($array) {
return (count(array_fi lter($array)) == 0) ? 1 : 0;
}

returns 1 if all elements are "" otherwise it returns 0.
--
Dave -
www.blurredistinction.com
http://www.macromedia.com/support/fo...am_macromedia/
Feb 11 '06 #2

Dave Mennenoh wrote:
...good one-liner...

This is what I came up with:

function isEmpty($array) {
return (count(array_fi lter($array)) == 0) ? 1 : 0;
}

returns 1 if all elements are "" otherwise it returns 0.
--
Dave -
www.blurredistinction.com
http://www.macromedia.com/support/fo...am_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_fi lter($array, $my_not_empty)) == 0) ? 1 : 0;
}

Feb 11 '06 #3
la***********@z ipmail.com wrote:
Hi,

Wondered if there was a good one-liner for what I want to do in PHP 4.
I have an array, with an arbitrary number of elements. I'd like to
know if all the elements in the array are empty. If at least one
element is non-empty, then the entire condition is false.


function isEmpty($array = array()) {
foreach ($array as $element) {
if (!empty($elemen t)) {
return false;
}
}
return true;
}

Okay, maybe not quite one-liner, but it should do the trick.
Ofcourse, it does depend if you agree with "empty"'s definition of what
empty is.

/Marcin
Feb 13 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2192
by: D. Alvarado | last post by:
Hi, I have this arr $months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); and I would like to take this array and build a string of the form "<option value=\"1\">January</option>\n<option value=\"2\">February</option>\n<option value=\"3\">March</option> ...
11
3264
by: Dr John Stockton | last post by:
Q1 : Given an array such as might have been generated by var A = is there a highly effective way of reducing it to - i.e. removing the undefineds and shifting the rest down? A.sort().slice(0,n) // would do it, but sorts; and the number
8
1984
by: Sowen | last post by:
Hi, I have an object "elem", there are only simple functions inside, like setName, getName, and three constructors Now I have another class "Base", need an array of elem to initialize class Base { public:
26
9632
by: JGH | last post by:
How can I check if a key is defined in an associative array? var users = new array(); users = "Joe Blow"; users = "John Doe"; users = "Jane Doe"; function isUser (userID) { if (?????)
5
1230
by: Paul | last post by:
Hi, I'm trying to create an array containing the entire contents of a text file and delimit the elements using vbCrLf. The text file is like this.... -text1 -text2 -text3
18
4780
by: staeri | last post by:
I'm using the following code to create a sum: forecast = forecast + eval(f.value); This only works if "f.value" contains a number. Can someone please help me so that it also works if "f.value" is empty or contains text. Regards,
4
7089
by: =?Utf-8?B?cm9nZXJfMjc=?= | last post by:
hey, I have a method that takes a char array of 10. I have a char array of 30. how do I make it send the first 10, then the next 10, then the final 10 ? I need help with my looping skills. thanks. I have this, but I get the index was out of bounds.... char char1 =
12
123755
by: zzapper | last post by:
Hi, Have tried to google this without 100% satisfaction. A function reads a mysql record into an array and returns it, if it doesnt exist it returns '' or FALSE. What ways do I have to test that my returned array is not empty -- zzapper
36
9180
by: laredotornado | last post by:
Hi, I'm using PHP 5. I have an array of strings. What is the simplest way to remove the elements that are empty, i.e. where the expression "empty($elt)" returns true? Thanks, - Dave
0
8983
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8822
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9528
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9359
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6792
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3298
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.