Monday 5 April 2021

php: Array: max, min: get maximum and minimum value of array

‘max’ and ‘min’ functions takes an array as argument and return maximum and minimum values of array.

 

Syntax

max($array_name);

min($array_name);

 

Example

$max_element = max($arr1);

$min_element = min($arr1);

 

array_max_min_demo.php

#!/usr/bin/php

<?php

    $arr1 = array(22, 31, -5, 77, -98, 18);
    
    $max_element = max($arr1);
    $min_element = min($arr1);

    print_r($arr1);
    echo "\nMaximum Element : $max_element\n";
    echo "\nMinimum Element : $min_element\n";
?>

 

Output

$./array_max_min_demo.php 

Array
(
    [0] => 22
    [1] => 31
    [2] => -5
    [3] => 77
    [4] => -98
    [5] => 18
)

Maximum Element : 77

Minimum Element : -98

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment