开发者

Simple PHP warning question?

I was wondering how can I correct this warning I keep getting listed below.

I'm using PHP & MySQL

Warning: min() [function.min]: Array must contain at least one element

Here is part of the code I think is causing the problem.

$tags = tag_info($link);

$minimum_count = min(array_values($tags));
$maximum_count = max(array_values($tags));
$spread = $maximum_count - $minimum_count;

I would of posted the whole code but some ignorant users will probably feel its a duplicate question so if you need to see the full code please look at the past questions thanks.

Okay I'm thinking its not this piece of code because every ones code i开发者_如何学Cs not displaying anything but its getting rid of the warning. You can see the full code here Full code


$tags = tag_info($link);

$spread = $tags ? max($tags) -  min($tags) : 0;

That code is valid as long as your tag_info() function returns an array.

PHP's built-in array_values() function is useless, since min() and max() both ignore the keys in an array.


    if( !empty( $tags ) )
    {
      $minimum_count = min( array_values( $tags ) ) ;
    }


$tags = tag_info($link);

if ( 
  is_array( $tags ) &&
  count( $tags ) > 0
) {
  $values = array_values( $tags );

  $spread = max( $values ) - min( $values );
} else
  $spread = 0;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜