SlaPtHiS
SlaPtHiS

Reputation: 1

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

Upvotes: 0

Views: 121

Answers (3)

Dor
Dor

Reputation: 7484

$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.

Upvotes: 2

silent
silent

Reputation: 3923

$tags = tag_info($link);

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

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

Upvotes: 0

bobobobo
bobobobo

Reputation: 67224

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

Upvotes: 0

Related Questions