SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    finding the smallest value in an Array

    Code:
    $myArray = array(48, 9, 52, 183);
    I have $myArray like the above.

    I like to find the smallest value in the Array.

    So I made the code below.


    Code:
    code
    
    if ( $myArray[0] < $myArray[1] ) {
    $smallest_between01=$myArray[0];
    } else {
    $smallest_between01=$myArray[1];
    } 
    
    
    if ( $myArray[2] < $myArray[3] ) {
    $smallest_between23=$myArray[2];
    } else {
    $smallest_between23=$myArray[3];
    } 
    
    
    
    if ( $smallest_between01 < $smallest_between23 ) {
    $smallest=$smallest_between01;
    } else {
    $smallest=$smallest_between23;
    } 
    
    echo $smallest;
    
    
    result
    
    9
    I now found the smallest value "9" which I wanted.

    I guess there might be more fine code for finding the smallest value in the Array.

    Do you have any?

    ( I am a beginner at Array.)

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •