Testing if an array is empty

This is a discussion on Testing if an array is empty within the PHP Language forums, part of the PHP Programming Forums category; Hi, Have tried to google this without 100% satisfaction. A function reads a mysql record into an array and returns ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-13-2007
zzapper
 
Posts: n/a
Default Testing if an array is empty

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
Best of VimTips
http://www.vim.org/tips/tip.php?tip_id=305

Reply With Quote
  #2 (permalink)  
Old 08-13-2007
R. Rajesh Jeba Anbiah
 
Posts: n/a
Default Re: Testing if an array is empty

On Aug 13, 8:32 pm, zzapper <zzap...@gmail.com> wrote:
> 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


http://in2.php.net/empty

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Reply With Quote
  #3 (permalink)  
Old 08-13-2007
Rik
 
Posts: n/a
Default Re: Testing if an array is empty

On Mon, 13 Aug 2007 17:32:33 +0200, zzapper <zzapper@gmail.com> wrote:
> 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


Something like the following?
<?php
if(empty($array)) echo '$array is empty';
?>

If you want an empty string, false, and an empty array handled seperately,
you can use the following:

<?php
if($return===false){ //notice the triple '='
echo 'Function returned false';
} else if($return === ''){
echo 'Function returned empty string.';
} else if(is_array($return) && empty($return)){
echo 'Function returned empty array.';
} else {
echo 'Function returned:';
var_dump($return);
}
?>

--
Rik Wasmus
Reply With Quote
  #4 (permalink)  
Old 08-13-2007
burgermeister01@gmail.com
 
Posts: n/a
Default Re: Testing if an array is empty

On Aug 13, 10:32 am, zzapper <zzap...@gmail.com> wrote:
> 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
> Best of VimTipshttp://www.vim.org/tips/tip.php?tip_id=305


The two immediate methods that pop into my head are as follows:

1.
if(count($array) == 0)

2.
if(empty($array))

There could be more, but these should do the job ok.


Reply With Quote
  #5 (permalink)  
Old 08-13-2007
Rik
 
Posts: n/a
Default Re: Testing if an array is empty

On Mon, 13 Aug 2007 17:32:33 +0200, zzapper <zzapper@gmail.com> wrote:
> --
> zzapper


BTW: your sig separator seems to be broken. It is supposed to be
dash-dash-space-newline. I'm missing the space over here (at least in
Opera), so your signature keeps getting in my quote.
--
Rik Wasmus
Reply With Quote
  #6 (permalink)  
Old 08-13-2007
zzapper
 
Posts: n/a
Default Re: Testing if an array is empty

On Aug 13, 4:42 pm, "R. Rajesh Jeba Anbiah"
<ng4rrjanb...@rediffmail.com> wrote:
> On Aug 13, 8:32 pm, zzapper <zzap...@gmail.com> wrote:
>
> > 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

>
> http://in2.php.net/empty


Hi
The problem I have with empty() is as follows


<?php
$fred=array ('');
if (empty($fred)) { echo "empty";}
else {echo 'not empty';}
?>

reports not empty?

zzapper

Reply With Quote
  #7 (permalink)  
Old 08-13-2007
burgermeister01@gmail.com
 
Posts: n/a
Default Re: Testing if an array is empty

On Aug 13, 10:57 am, zzapper <zzap...@gmail.com> wrote:
> On Aug 13, 4:42 pm, "R. Rajesh Jeba Anbiah"
>
> <ng4rrjanb...@rediffmail.com> wrote:
> > On Aug 13, 8:32 pm, zzapper <zzap...@gmail.com> wrote:

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

>
> > http://in2.php.net/empty

>
> Hi
> The problem I have with empty() is as follows
>
> <?php
> $fred=array ('');
> if (empty($fred)) { echo "empty";}
> else {echo 'not empty';}
> ?>
>
> reports not empty?
>
> zzapper


This is because the array technically has one element that is equal to
''. To deal with this you will probably either need to clean the array
first, removing any elements that are equal to '', or checking each
element as you process the array and handle the empty ones differently.

Reply With Quote
  #8 (permalink)  
Old 08-13-2007
zzapper
 
Posts: n/a
Default Re: Testing if an array is empty

On Aug 13, 4:47 pm, Rik <luiheidsgoe...@hotmail.com> wrote:
> On Mon, 13 Aug 2007 17:32:33 +0200, zzapper <zzap...@gmail.com> wrote:
> > --
> > zzapper

>
> BTW: your sig separator seems to be broken. It is supposed to be
> dash-dash-space-newline. I'm missing the space over here (at least in
> Opera), so your signature keeps getting in my quote.
> --
> Rik Wasmus


I'm posting & viewing from Google groups, if you view source the page
my dash-dash-space-newline appears to be there.
But you are not the first to complain, so can somebody explain.
zzapper

Reply With Quote
  #9 (permalink)  
Old 08-13-2007
Rik
 
Posts: n/a
Default Re: Testing if an array is empty

On Mon, 13 Aug 2007 17:57:18 +0200, zzapper <zzapper@gmail.com> wrote:

> On Aug 13, 4:42 pm, "R. Rajesh Jeba Anbiah"
> <ng4rrjanb...@rediffmail.com> wrote:
>> On Aug 13, 8:32 pm, zzapper <zzap...@gmail.com> wrote:
>>
>> > 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

>>
>> http://in2.php.net/empty

>
> Hi
> The problem I have with empty() is as follows
>
>
> <?php
> $fred=array ('');
> if (empty($fred)) { echo "empty";}
> else {echo 'not empty';}
> ?>
>
> reports not empty?


Indeed. An array with zero values is empty, an array with values (be it
NULL, false, whatever) is not.

If you want to know wether an array consists only of 'empty' values (does
_not_ work recursively):

<?php
//sorry, can't think of a good name:
function only_empty_array_values($array){
$filtered = array_filter($array,'_not_empty');
return empty($filtered);
}
function _not_empty($var){
return !empty($var);
}
$ar = array('',false,null);
$check = only_empty_array_values($ar);
?>

Allthough it could be better, depending on circumstances, to rewrite the
function that returns this 'array of empties' to one that simply returns
false instead of the array if there are no results.
--
Rik Wasmus
Reply With Quote
  #10 (permalink)  
Old 08-13-2007
Jerry Stuckle
 
Posts: n/a
Default Re: Testing if an array is empty

zzapper wrote:
> On Aug 13, 4:47 pm, Rik <luiheidsgoe...@hotmail.com> wrote:
>> On Mon, 13 Aug 2007 17:32:33 +0200, zzapper <zzap...@gmail.com> wrote:
>>> --
>>> zzapper

>> BTW: your sig separator seems to be broken. It is supposed to be
>> dash-dash-space-newline. I'm missing the space over here (at least in
>> Opera), so your signature keeps getting in my quote.
>> --
>> Rik Wasmus

>
> I'm posting & viewing from Google groups, if you view source the page
> my dash-dash-space-newline appears to be there.
> But you are not the first to complain, so can somebody explain.
> zzapper
>


I just viewed the source in your original message. Rik is correct. The
trailing space is missing. All you have is dash-dash-newline.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:19 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0