3

This works but, is it reliable? Will it always work since the MAINarray is containing ANOTHERarray which contains 2 values and min should work in MAINarray and find lowest value in its subarays.

$a[0]=array(0=>522,1=>'Truck');
$a[1]=array(0=>230,1=>'Bear');
$a[2]=array(0=>13,1=>'Feather');
$a[3]=array(0=>40,1=>'Rabit');
$z=min($a);$y=max($a);
echo "The easiest is ".$z[1]." with only ".$z[0]." pounds!<br>";
echo "The heaviest is ".$y[1]." with ".$y[0]." pounds!";

What you say?

3
  • i say testing it your self will yield the most beneficial results. Commented Feb 13, 2011 at 19:26
  • It's a hack. Good for a fix, but nothing to build upon. Commented Feb 13, 2011 at 19:26
  • @Dagon while i was writing it i tested it :D lol @user.... i have tested it with 10,000 of data and always it works good. Commented Feb 13, 2011 at 19:42

4 Answers 4

3

Yes, it is reliable. It's safe to assume that min(array(1, 2, ..., n)) is equivalent to min(1, 2, ..., n), and the documentation specifically covers how min compares multiple arrays:

// With multiple arrays, min compares from left to right
// so in our example: 2 == 2, but 4 < 5
$val = min(array(2, 4, 8), array(2, 5, 1)); // array(2, 4, 8)
Sign up to request clarification or add additional context in comments.

Comments

2

My understanding of how min works with your type of input is:

  1. Only consider the arrays with the fewest number of items.
  2. Compare the first elements
  3. If there is a tie, compare the next element of each array. Repeat step.

e.g.:

array(
  array(1, "A"),
  array(2),        // min
)


array(
  array(1, "A"),  // min
  array(2, "A"),        
)

array(
  array(1, "Z"),  
  array(1, "A"),  // min
)

I don't have a source for that information, but it's how I remember it working.

1 Comment

right... i have just tried it... and you are right $a[0]=array(0=>522,1=>'Truck'); $a[1]=array(0=>230,1=>'Bear',2=>'au'); truck is easier than bear... Thank you konforce! ;)
1

I’m not sure if this always works. In case of doubt just implement the function yourself:

function extremes($array, $key=0) {
    if (count($array) === 0) return null;
    $min = $max = null;
    foreach ($array as &$val) {
        if (!isset($val[$key])) continue;
        if ($min > $val[$key]) $min = $val[$key];
        else if ($max < $val[$key]) $max = $val[$key];
    }
    return array($min, $max);
}

$a = array(array(522, 'Truck'), array(230, 'Bear'), array(13, 'Feather'), array(40, 'Rabit'));
list($z, $y) = extremes($a);

1 Comment

its the same what min and max function does... isnt it?
0

The only evidence I could find to say it was intended to be able to use an array of arrays to compare is this bug report to improve the php docs. http://bugs.php.net/bug.php?id=50607

Although, it's quite unlikely for them to remove this functionality because of the way it has to compare arrays already by going into them recursively. The arguments list itself when using the function normally would be an array because there is no finite number of arguments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.