简体   繁体   中英

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 is 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM