• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / php max – Find Maximum Value of Array in php

php max – Find Maximum Value of Array in php

December 13, 2021 Leave a Comment

To find the maximum value of an array in php, we can use the php max() function.

$max = max($values)

The max function returns the “highest” value according to standard comparisons in php. If there are multiple values which have the same value, then the first one will be returned.


To get the maximum of an array in php, we use the php max() function.

Let’s say we have the following array of integers:

$sample_array = array(0,4,1,-5)

We can easily get the maximum of this array by calling the php max() function.

$sample_array = array(0,4,1,-5);

echo max($sample_array); // -5

php max() for Arguments of Different Types

The php max() function is able to take arguments of different types and allows us to compare between integers, strings, and arrays.

If you want to use the php max() function to compare an array with multiple types, you need to understand how the max() function works with these different types.

For strings, the max() function assigns a value of 0.

So if I call the php max() function with the following arguments, the returned value will be “example string”, because 1 is greater than 0.

echo max("example string",1);  // 1 

If we want to compare arrays with other arrays or values, then there are a few things we need to keep in mind.

First, the longer array is always greater than a shorter array. Second, if they arrays are the same length, then we go from left to right and if there is a difference in elements, the array which contains this element is returned as the minimum. Third, if an array is compared against an integer or string, the array is always bigger.

We can see this below in the following examples of using the php max() method with arrays:

echo json_encode(max(array(0,3,4),array(0,5,1)));  // [0,5,1] because 5 > 3 
echo json_encode(max(array(0,3,4),array(-10,0,5,1)));  // [-10,0,5,1] because the array is longer than [0,3,4]
echo json_encode(max(array(0,3,4),"example_string"));  // [0,3,4] because arrays are always bigger

If you want to compare boolean variables, you can do this, but the results will not necessarily make sense.

Using php max() to Find the Maximum Value Between Dates

The max() function can also find the maximum value between dates. All we need to do is pass our dates to the function.

$dt1 = new DateTime('2021-12-07 18:34', new DateTimeZone('UTC'));
$dt2 = new DateTime('2021-12-03 11:34', new DateTimeZone('UTC'));
echo max($dt1,$dt2)->format(DateTime::RFC3339) // 2021-12-07T18:34:00+00:00

Hopefully this post has been beneficial for you to understand how you can calculate the maximum of an array in php with the php max() function.

Other Articles You'll Also Like:

  • 1.  php array_combine() – Create New Array Given Arrays of Keys and Values
  • 2.  php print_r() Function- Get Information about Variables in php
  • 3.  Get Days in Month Using php
  • 4.  php Move File from One Directory to Another Folder
  • 5.  How to Check If String Contains Substring in PHP
  • 6.  php is_string() Function – Check if Variable is String in php
  • 7.  php str_split() – Convert String into Array of Equal Length Substrings
  • 8.  php is_float() Function – Check if Variable is Float in php
  • 9.  php cosh – Find Hyperbolic Cosine of Number Using php cosh() Function
  • 10.  Replace Space with Dash in php String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy