Forum Moderators: coopster

Message Too Old, No Replies

Array must contain at least one element in it

         

hanyaz

10:02 am on Nov 15, 2008 (gmt 0)

10+ Year Member



Hello,
i am using a php class where one array is required to perform some calculation. However the array is generated on the fly, it is a list of keywords...and sometimes no keywords are detected so the class requires them to perform an action. I want to introduce a condition to avoid errors if the list of keywords is not generated. Here is the piece of code :

$this->minTagValue = min($this->KeywordsArray);
$this->maxTagValue = max($this->KeywordsArray);
$this->FontRatio = ($this->MaxFontSize - $this->MinFontSize) / ($this->maxTagValue - $this->minTagValue) ;
$this->FontOffset = $this->MaxFontSize - ($this->FontRatio * $this->maxTagValue );

I got the following errors :

Warning: min() [function.min]: Array must contain atleast one element
max() [function.max]: Array must contain atleast one element
Warning: Division by zero

to avoid the error i tried :

if ($this->KeywordsArray > 0) {
$this->minTagValue = min($this->KeywordsArray);
$this->maxTagValue = max($this->KeywordsArray);

$this->FontRatio = ($this->MaxFontSize - $this->MinFontSize) / ($this->maxTagValue - $this->minTagValue) ;
$this->FontOffset = $this->MaxFontSize - ($this->FontRatio * $this->maxTagValue );
}
else {
exit ('');
}

Could someone help me to fix this ?
Thanks
hanyaz

Anyango

11:02 am on Nov 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if(is_array($this->KeywordsArray) && count($this->KeywordsArray)>0)
{ //array check starts

$this->minTagValue = min($this->KeywordsArray);
$this->maxTagValue = max($this->KeywordsArray);
$this->FontRatio = ($this->MaxFontSize - $this->MinFontSize) / ($this->maxTagValue - $this->minTagValue) ;
$this->FontOffset = $this->MaxFontSize - ($this->FontRatio * $this->maxTagValue );

} //array check ends
else
{
die("Not an Array");
}

try that, you can ofcourse move the "aray check end" to an upper line if You ve to.

[edited by: Anyango at 11:07 am (utc) on Nov. 15, 2008]

hanyaz

11:22 am on Nov 15, 2008 (gmt 0)

10+ Year Member



Thanks Anyango, this did the job !
Regards
hanyaz