473,326 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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_arr); // should contain the value TRUE

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

Any slick ways to find this out? - Dave

Feb 11 '06 #1
3 34856
....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
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_filter($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_filter($array, $my_not_empty)) == 0) ? 1 : 0;
}

Feb 11 '06 #3
la***********@zipmail.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($element)) {
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
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...
11
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? ...
8
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...
26
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
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
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...
4
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....
12
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...
36
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.