note 91437 deleted from function.max by danbrown

From: Date: Wed, 10 Jun 2009 12:38:53 +0000
Subject: note 91437 deleted from function.max by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-156735@lists.php.net to get a copy of this message
Note Submitter: Alexander=dot=Bouz=at=gmx=dot=at ---- sumeruter stated that max() would return the subarray containing the highest value. http://php.net/manual/en/function.max.php#87547 This is not correct. The decision of max() is made according to the *first* element within the first nested array. An example, borrowed from sumeruter: <?php $depth3=array( array(array(1,6,3),array(7,2,5),array(9,3,0)), array(array(2,1,8),array(4,4,5),array( 99 ,6,7)), array(array(0,3,2),array(5,91,11),array(42,25,12))); // it is indeed 99 echo max(max(max($depth3))); ?> The same example, slightly modified: <?php // I just switched the first value of the nested arrays $depth3=array( array(array(2,6,3),array(7,2,5),array(9,3,0)), array(array(1,1,8),array(4,4,5),array( 99 ,6,7)), array(array(0,3,2),array(5,91,11),array(42,25,12))); // Whoops, this echoes 9 echo max(max(max($depth3))); ?> This is because in the first nested max(), the function decides that 2 > 1 > 0 and returns the array array(array(2,6,3),array(7,2,5),array(9,3,0)) which doesn't contain our highest value. For this purpose, better use array_walk_recursive(). http://php.net/manual/en/function.array-walk-recursive.php

« previous php.notes (#156735) next »