php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #50607 max() should say more about multi-dimensional arrays
Submitted: 2009-12-29 16:44 UTC Modified: 2010-01-12 11:06 UTC
From: thiago@php.net Assigned:
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: thiago@php.net
New email:
PHP Version: OS:

 

 [2009-12-29 16:44 UTC] thiago@php.net
Description:
------------
I'm removing a few notes from max() docs because I think some of it's 
content should be added to docs.

Note #87547 from sumeruter+php add an at gmail dot com:
"An interesting feature not mentioned is that if a recursive array is 
passed to max, max will return the sub-array containing the highest 
value.

Example:
<?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)));

$depth2=max($depth3);
// = array(array(2,1,8),array(4,4,5),array(  99  ,6,7))

$depth1=max($depth2);
// = array(  99  ,6,7)

$depth0=max($depth1);
// = 99

echo max(max(max($depth3)));
// echo 99;
?>"

Note #92073 from soapergem at gmail dot com: 
A quick addendum to the comment left by "sumeruter": the recursive 
property will only work if all the sub-arrays are exactly the same 
length. Otherwise it will just return the longest array every time.

Note #95367 from vendelin8 at gmail dot com:
I disagree with sumeruter that in case of a recursive array php 
returnes the sub-array containing the highest value recursively. It 
checks the first value.
Eg.

$array = array(array(2, 3, 4), array(1, 4, 5));
print_r(max($array));

result:
Array ( [0] => 2 [1] => 3 [2] => 4 )

In sumeruter's example the maximal value was always in the array that 
has the highest first value as well.

Expected result:
----------------
This is the example of information I think should be added in docs:

When a multi-dimensional array is given, this function returns the sub-
array with highest first value.

In order to copare multi-dimensional arrays, they should have the exact 
same size. (I could not test to ensure this behavior tho)



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-01-12 11:04 UTC] svn@php.net
Automatic comment from SVN on behalf of degeberg
Revision: http://svn.php.net/viewvc/?view=revision&revision=293443
Log: Clarified comparison rules for max(). Closes PHP bug #50607 (max() should say more about multi-dimensional arrays)
 [2010-01-12 11:06 UTC] degeberg@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Dec 16 13:00:01 2025 UTC