Skip to content
  • Homepage
  • HTML
  • CSS
  • Symfony
  • PHP
  • How to
  • Contact
  • Donate

Teach Developer

Articles, Guides & Tips

How to PHP Get Max Value From Array

Home  »  How to • PHP • Tips   »   How to PHP Get Max Value From Array
Posted on September 18, 2022September 18, 2022
908

PHP gets the max/maximum/largest value from the array. This tutorial has the purpose to explain to you several easy ways to find or get the maximum or largest value from an array in PHP.

Here, we will take e.g. PHP gets the maximum value in an array, get the largest number in the array PHP without a function or get the max value from an array in PHP using for loop

PHP finds the highest value in an array

We can use the PHP max() function, which is used to find the largest number from an array in PHP.

Before we use the take examples, you must know about the PHP max() function.

PHP max() function

The max() function is an inbuilt PHP function, which is used to find the numerically maximum or highest value in an array or find the maximum or highest value of given specified values.

Syntax

 max(array)  
 or
 max(value1, value2, value3 … valueN)

Example – 1 Get max value in array PHP using max() function

Let’s take the first example, we will use the PHP max() function to find the maximum value in the array. Let’s see the example below:

<?php
$array = [1, 10, 50, 40, 2, 15, 100];
$res = max($array);
 
print_r($res);
 
?> 

The output of the above program is: 100

Example – Find the largest number in array PHP without function

Let’s take the second example, in this example, we will find or get the largest or maximum number in the array PHP without function. Let’s see the example below:

<?php
 
$array = array(1000,400,10,50,170,500,45);
 
$max = $array[0];
 
foreach($array as $key => $val) {
 
    if($max < $val) {
 
        $max = $val;
         
    }
}
print $max;
 
?> 

The output of the above program is: 1000

Example – 3 PHP get max or highest value in array using for loop

Let’s take the third example, to find the maximum or largest, or highest value in array PHP without using any function. Let’s look at the examples below:

<?php 
$array = array(500, 20, 55 ,165, 456, 78, 75);
 
$max = 0;
 
for($i=0;$i<count($arr);$i++)
 {
    if ($arr[$i] > $max)
    {
        $max = $arr[$i];
    }
}
 
print $max;
 
?>

Conclusion

Through this tutorial, we have learned how to find or get max value or elements from an array in PHP.

How to, PHP, Tips Tags:PHP

Post navigation

Previous Post: How to Recover Deleted WhatsApp Messages
Next Post: How to Send Email from Localhost in PHP

Related Posts

  • How to use :hover to modify the CSS of another class?
  • PHP 8: Constructor property promotion
  • How to use events listeners and Event Subscriber in Symfony
  • How to check if a customer is logged in to Magento 2 or not?
  • How to Symfony Request
  • How to Symfony parameters and environment variables use

Categories

  • Codeigniter (3)
  • CSS (11)
  • eCommerce (1)
  • Framework (1)
  • Git (3)
  • How to (43)
  • HTML (5)
  • JavaScript (15)
  • Jquery (7)
  • Laravel (1)
  • Linux (4)
  • Magento-2 (1)
  • Node js (4)
  • Others (2)
  • PHP (11)
  • React (13)
  • Server (1)
  • SSH (3)
  • Symfony (6)
  • Tips (16)
  • Top Tutorials (10)
  • Ubuntu (3)
  • Vue (1)
  • Wordpress (7)

Latest Posts

  • What is SSH in Linux?
  • How to Delete Files in Ubuntu Command Line
  • How to Deploy a React application on a cPanel
  • How to use events listeners and Event Subscriber in Symfony
  • How to Convert PHP CSV to JSON

WEEKLY TAGS

AJAX (1) Codeigniter (1) Javascript (11) JQuery (1) PHP (16) Programming (1) React (3) Symfony (1)

Random Post

How to Get Environment Variables in Laravel ReactJS
How to Deploy a React application on a cPanel
How to install Git on Linux
How to change background Opacity when the bootstrap modal is open with CSS?
How to make the scrollbar visible in mobile browsers with CSS?

Quick Navigation

  • About
  • Contact
  • Privacy Policy

© Teach Developer 2021. All rights reserved