|  | Posted by Dan Trainor on 07/21/05 23:51 
Mike Johnson wrote:> From: Dan Trainor [mailto:info@hostinthebox.net]
 >
 >
 >>Hello, all -
 >>
 >>I've been looking around for a function that would tell me if a $value
 >>in a $key=>$value array was empty, and I could not find one.  So I
 >>decided to make my own.  Even if I am re-inventing the wheel,
 >>I thought that the practice might be good for me.
 >>
 >>However, my function doesn't *quite* work, and I'm having a difficult
 >>time finding out why.  The code is as follows:
 >>
 >>function findMissingVals($workingArray) {
 >>	$newcount = count($workingArray);
 >>	for ($i = 0; $i <= $newcount; $i++) {
 >>		if (empty($workingArray['$i'])) {
 >>			return 1;
 >>		}
 >>	}
 >>}
 >>
 >>So it takes in $workingArray as an array, runs a loop, checks $i, yada
 >>yada.  The thing is, that sometimes the function does not
 >>return 1, even when it should.
 >>
 >>I was hoping some experienced eyes could take a gander at
 >>this and give me some pointers.
 >
 >
 > PHP doesn't eval code in single-quotes, so what you want to do is
 > simply:
 >
 > if (empty($workingArray[$i])) {
 > 	return 1;
 > }
 >
 > With the single-quotes, it's looking for the string $i as a key.
 >
 > HTH!
 >
 
 
 Hey there, Mike -
 
 Your tips were very helpful, thank you.  I saw my error, but I am still
 having problems.  Being somewhat novice to PHP, I think my error might
 very well just be in my implementation of the function, as follows:
 
 function findMissingVals($workingArray) {
 $newcount = count($workingArray);
 for ($i = 0; $i <= $newcount; $i++) {
 if (empty($workingArray[$i])) {
 return 1;
 }
 }
 }
 
 if (findMissingVals($vars)) {
 if (!$var1)	{ ?> hi1 <? };
 if (!$var2)	{ ?> hi2 <? };
 if (!$var3)	{ ?> hi3 <? };
 if (!$var4)	{ ?> hi4 <? };
 if (!$var5)	{ ?> hi5 <? };
 if (!$var6)	{ ?> hi6 <? };
 } else {
 echo "hi";
 }
 
 
 I never see "hi", even if I have an array as $vars as such:
 
 $vars = array("one","two","","four","five");
 
 so I'm a bit confused.
 
 If you wouldn't mind taking another few minutes here, I would greatly
 appreciate it.
 
 Thanks!
 -dant
 [Back to original message] |