This is a discussion on Testing if an array is empty within the PHP Language forums, part of the PHP Programming Forums category; On 13.08.2007 17:32 zzapper wrote: > Hi, > Have tried to google this without 100% satisfaction. > &...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
On Mon, 13 Aug 2007 18:36:20 +0200, gosha bine <stereofrog@gmail.com>
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 |
|
|||
|
On Aug 13, 5:39 pm, Rik <luiheidsgoe...@hotmail.com> wrote:
> On Mon, 13 Aug 2007 18:36:20 +0200, gosha bine <stereof...@gmail.com> > 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 |
|
|||
|
On Mon, 13 Aug 2007 22:33:27 +0200, zzapper <zzapper@gmail.com> wrote:
> On Aug 13, 5:39 pm, Rik <luiheidsgoe...@hotmail.com> wrote: >> On Mon, 13 Aug 2007 18:36:20 +0200, gosha bine <stereof...@gmail.com> >> 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 |