Testing if an array is empty

Testing if an array is empty

am 13.08.2007 17:32:33 von zzapper

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

Re: Testing if an array is empty

am 13.08.2007 17:42:18 von ng4rrjanbiah

On Aug 13, 8:32 pm, zzapper 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

--

Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Re: Testing if an array is empty

am 13.08.2007 17:42:42 von luiheidsgoeroe

On Mon, 13 Aug 2007 17:32:33 +0200, zzapper 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?
if(empty($array)) echo '$array is empty';
?>

If you want an empty string, false, and an empty array handled seperatel=
y, =

you can use the following:

if($return===3Dfalse){ //notice the triple '=3D'
echo 'Function returned false';
} else if($return ===3D ''){
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

Re: Testing if an array is empty

am 13.08.2007 17:44:18 von burgermeister01

On Aug 13, 10:32 am, zzapper 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.

Re: Testing if an array is empty

am 13.08.2007 17:47:38 von luiheidsgoeroe

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

Re: Testing if an array is empty

am 13.08.2007 17:57:18 von zzapper

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


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

reports not empty?

zzapper

Re: Testing if an array is empty

am 13.08.2007 18:04:38 von burgermeister01

On Aug 13, 10:57 am, zzapper wrote:
> On Aug 13, 4:42 pm, "R. Rajesh Jeba Anbiah"
>
> wrote:
> > On Aug 13, 8:32 pm, zzapper 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
>
> > $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.

Re: Testing if an array is empty

am 13.08.2007 18:08:16 von zzapper

On Aug 13, 4:47 pm, Rik wrote:
> On Mon, 13 Aug 2007 17:32:33 +0200, zzapper 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

Re: Testing if an array is empty

am 13.08.2007 18:10:59 von luiheidsgoeroe

On Mon, 13 Aug 2007 17:57:18 +0200, zzapper wrote:

> On Aug 13, 4:42 pm, "R. Rajesh Jeba Anbiah"
> wrote:
>> On Aug 13, 8:32 pm, zzapper 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
>
>
> > $fred=3Darray ('');
> 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 (doe=
s =

_not_ work recursively):

//sorry, can't think of a good name:
function only_empty_array_values($array){
$filtered =3D array_filter($array,'_not_empty');
return empty($filtered);
}
function _not_empty($var){
return !empty($var);
}
$ar =3D array('',false,null);
$check =3D 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

Re: Testing if an array is empty

am 13.08.2007 18:32:59 von Jerry Stuckle

zzapper wrote:
> On Aug 13, 4:47 pm, Rik wrote:
>> On Mon, 13 Aug 2007 17:32:33 +0200, zzapper 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
==================

Re: Testing if an array is empty

am 13.08.2007 18:36:20 von gosha bine

On 13.08.2007 17:32 zzapper 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


if(array_filter($ary))
$ary contains at least one non-empty element
else
$ary contains 0 elements or all elements are empty


--
gosha bine

makrell ~ http://www.tagarga.com/blok/makrell
php done right ;) http://code.google.com/p/pihipi

Re: Testing if an array is empty

am 13.08.2007 18:39:56 von luiheidsgoeroe

On Mon, 13 Aug 2007 18:36:20 +0200, gosha bine
wrote:
> On 13.08.2007 17:32 zzapper 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
>
>
> if(array_filter($ary))
> $ary contains at least one non-empty element
> else
> $ary contains 0 elements or all elements are empty


Hmmz, didn't know this would work, but offcourse simple converting to
boolean would do the trick, away with my functions :)
--
Rik Wasmus

Re: Testing if an array is empty

am 13.08.2007 22:33:27 von zzapper

On Aug 13, 5:39 pm, Rik wrote:
> On Mon, 13 Aug 2007 18:36:20 +0200, gosha bine
> wrote:
>
> > On 13.08.2007 17:32zzapperwrote:
> >> 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
>
> > if(array_filter($ary))
> > $ary contains at least one non-empty element
> > else
> > $ary contains 0 elements or all elements are empty
>
Like it

Are we going to vote this best solution?

zzapper

Re: Testing if an array is empty

am 13.08.2007 22:34:50 von luiheidsgoeroe

On Mon, 13 Aug 2007 22:33:27 +0200, zzapper wrote:

> On Aug 13, 5:39 pm, Rik wrote:
>> On Mon, 13 Aug 2007 18:36:20 +0200, gosha bine
>> wrote:
>>
>> > On 13.08.2007 17:32zzapperwrote:
>> >> 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
>>
>> > if(array_filter($ary))
>> > $ary contains at least one non-empty element
>> > else
>> > $ary contains 0 elements or all elements are empty
>>
> Like it
>
> Are we going to vote this best solution?
>

Except it might be better for the function to return false instead of an
empty array, yes indeed, I'd say so :)

--
Rik Wasmus