How do i find the smallest and largest number in an array in php?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given an array of integers, find the maximum and minimum in it.
    Examples: 
     

    Input : arr[] = {2, 3, 1, 6, 7}
    Output : Maximum integer of the given array:7
             Minimum integer of the given array:1
    
    Input  : arr[] = {1, 2, 3, 4, 5}
    Output : Maximum integer of the given array : 5
             Minimum integer of the given array : 1

    Approach 1 [Simple] : We simply traverse through the array, find its maximum and minimum. 
     

    PHP

    Output: 
     

    5
    1

    Approach 2 [Using Library Functions] : We use library functions to find minimum and maximum.

    1. Max[]:max[] returns the parameter value considered “highest” according to standard comparisons. If multiple values of different types evaluate as equal [e.g. 0 and ‘abc’] the first provided to the function will be returned. 
       
    2. Min[]:min[] returns the parameter value considered “lowest” according to standard comparisons. If multiple values of different types evaluate as equal [e.g. 0 and ‘abc’] the first provided to the function will be returned.

    PHP

    Output: 
     

    5
    1

    In this article, we show how to find the smallest number in an array in PHP.

    To find the smallest number in an array in PHP, we use the min[] function.

    Let's say we have the array of numbers below:

    $numbers= array[170, 210, 103, 375, 315, 470, 255];

    Let's say these numbers represent prices of airline tickets that a user looks up. To find the cheapest price, we need to be able to pick out the smallest number in the array.

    We use the min[] function to find the smallest number in the array.

    The general form to find the smallest number in an array is:

    $smallest= max[$array_name];

    where $array_name is the name of the array and $smallest is the variable which contains the lowest numerical value of the array.

    For the above array we created, the code to find the smallest number is:

    $smallest= min[$numbers];

    Actual PHP Output

    The smallest value in the array is 103

    Finding the smallest value in an array is very crucial for many applications. With finding the smallest value, we can find the cheapest price, meal, airline tickets, hotel, taxi fare, etc.

    How do I find the largest and smallest number in an array in php?

    We use the min[] function to find the smallest number in the array. The general form to find the smallest number in an array is: $smallest= max[$array_name]; where $array_name is the name of the array and $smallest is the variable which contains the lowest numerical value of the array.

    How do you find the largest and smallest numbers in an array?

    Algorithm to find the smallest and largest numbers in an array.
    Input the array elements..
    Initialize small = large = arr[0].
    Repeat from i = 2 to n..
    if[arr[i] > large].
    large = arr[i].
    if[arr[i] < small].
    small = arr[i].
    Print small and large..

    How do you find the minimum and maximum value in php?

    Approach 2 [Using Library Functions] : We use library functions to find minimum and maximum..
    Max[]:max[] returns the parameter value considered “highest” according to standard comparisons. ... .
    Min[]:min[] returns the parameter value considered “lowest” according to standard comparisons..

    How do you find the largest number in an array in php?

    The max[] function of PHP is used to find the numerically maximum value in an array or the numerically maximum value of several specified values. The max[] function can take an array or several numbers as an argument and return the numerically maximum value among the passed parameters.

    Chủ Đề