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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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/ |
|
|||
|
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 |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 ================== |