smallest number but not zero

Discussion in 'PHP' started by bugcoder, Feb 15, 2009.

  1. #1
    im feeling dont know what but i am stuck on this problem:confused:....please help
    how can i get the smallest number from these six variables

    $Price10 =23;
    $Price14 =43;
    $Price18 =29;
    $PriceSLV =0;
    $PriceGF10 =0;
    $PriceGF20 =0;

    but it should not be zero. i used min php function but it gives zero as result
     
    bugcoder, Feb 15, 2009 IP
  2. bugcoder

    bugcoder Peon

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i have used this sort of logic

    $arr = array(44,0,0,12,0,4);

    sort($arr);
    if($arr[0]!=0)
    {
    print_r($arr[0]);
    }
    elseif($arr[1]!=0)
    {
    print_r($arr[1]);
    }
    elseif($arr[2]!=0)
    {
    print_r($arr[2]);
    }
    elseif($arr[3]!=0)
    {
    print_r($arr[3]);
    }
    elseif($arr[4]!=0)
    {
    print_r($arr[4]);
    }
    else{
    print_r($arr[5]);
    }
     
    bugcoder, Feb 15, 2009 IP
  3. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #3
    An idea:

    <?php
    /*
    $Price10 =23;
    $Price14 =43;
    $Price18 =29;
    $PriceSLV =0;
    $PriceGF10 =0;
    $PriceGF20 =0;
    */
    
    //instead of using seperate variable, why dont use array?
    
    $price = array ( // where $price[0] = your $Price10
    0 => 23,
    43,
    29,
    0,
    0,
    0
    );
    
    sort($price,SORT_NUMERIC);
    
    $price = array_unique($price);
    
    if ($price[0] == 0) {
    $lowest = 1; // 1 = the index of array that carry the lowest value 
    } else {
    $lowest = 0; // 0 = the index of array that carry the lowest value 
    }
    
    //to use so the lowest value
    $use = $price[$lowest];
    
    // debug
    //var_dump($price);
    
    ?>
    
    
    PHP:
     
    ads2help, Feb 15, 2009 IP
  4. bugcoder

    bugcoder Peon

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    nice suggestion dear...thanks for this soloution as well :)
     
    bugcoder, Feb 15, 2009 IP