SitePoint Sponsor

User Tag List

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

    Finding the 2nd smallest value in an Array

    Code:
    min(array(48, 9, 52, 183));
    I can find the smallest value in an Array with the code above which is helped by guido2004 and logic_earth in the former post.

    Now I like to find the 2nd smallest value which is 48 in the Array.

    I guess I have 2 ways.

    The 1st way is that finding the 2nd smallest value directly if there is any code or function in PHP.

    The other way is that creates a new Array with the removing or deleting the smallest value which is 9 in the Array.
    The new Array will be newArray[48, 52, 183) and the code below will find my target value 48.
    Code:
    min(array(48, 52, 183));
    How can I find the 2nd smallest value directly(the 1st way)?
    How can I get remove an array value which is 9 for finding the second smallest value(the other way)?

    How can I get my target result?

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    sort$arraySORT_NUMERIC );
    $smallest array_shift$array );
    $smallest_2nd array_shift$array ); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,211
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much, logic_earth.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    use sort() and access the 2nd array item.

    PHP Code:
    $a=48;
    $b=9;
    $c=52;
    $d=183;
    $e = array($a$b$c$d);
    sort$e );

    echo 
    $e[1]; // 48 


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
  •