Results 1 to 8 of 8

Thread: Array Value

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default Array Value

    Using the php min array function is there a way to get the position that the min value came from? I was thinking of something with the count but can't seem to get it. For an example
    PHP Code:
    $value[0]= 12.84930;
    $value[1]=9989.12123;
    $value[2]=.0023412093;
    $value[3]=10.19;
    $value[4]=2.56;

    echo 
    min($value); //But instead of .0023412093 it returns 2. 

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You could do it in two steps:
    Get that min value, then find the key from that resulting value.
    http://www.php.net/manual/en/function.array-search.php

    Directly, though, I'm not sure how that would work.

    You could also use sort() or another similar function and then of course the key would be 0 for that lowest value.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    PHP Code:
    $value = array();
    $value[0]= 12.84930;
    $value[1]=9989.12123;
    $value[2]=.0023412093;
    $value[3]=10.19;
    $value[4]=2.56;

    echo 
    array_search(min($value), $value); // This will return the key, 2 in this case. 
    I don't think there is a one shot function. You'll have to combine at least two core php functions.
    Last edited by JasonDFR; 09-30-2009 at 07:50 AM. Reason: Misunderstood question

  4. #4
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    This follows on from what djr33 had to say

    PHP Code:
    <?php
    $value
    [0]= 12.84930;
    $value[1]=9989.12123;
    $value[2]=.0023412093;
    $value[3]=10.19;
    $value[4]=2.56;
    $val=min($value);

    $key=array_search($val$value);
    echo 
    "position = ".$key;
    ?>

  5. The Following User Says Thank You to forum_amnesiac For This Useful Post:

    bluewalrus (10-01-2009)

  6. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    And a correction: this will return the first key that matches the value, so if there are duplicates that might get confusing.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #6
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    I really enjoy problems like this.

    PHP Code:
    $value = array();
    $value[0]= 12.84930;
    $value[1]=9989.12123;
    $value[2]=.0023412093;
    $value[3]=10.19;
    $value[4]=2.56;
    $value[5]=.0023412093;
    $value[6]=.0023412093;

    function 
    minValueKeys($array) {
        
        
    $minValueKeys = array();
        
    $minValue min($array);
        do {
            
    $key array_search($minValue$array);
            
    $minValueKeys[] = $key;
            unset(
    $array[$key]);
        } while (
    in_array($minValue$array));
        
        return 
    $minValueKeys;    
    }

    var_dump(minValueKeys($value)); 
    ...and a perfect problem for the seldom used do ... while loop! I love it.

  8. #7
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    Quote Originally Posted by bluewalrus View Post
    Using the php min array function is there a way to get the position that the min value came from? I was thinking of something with the count but can't seem to get it. For an example
    PHP Code:
    $value[0]= 12.84930;
    $value[1]=9989.12123;
    $value[2]=.0023412093;
    $value[3]=10.19;
    $value[4]=2.56;

    echo 
    min($value); //But instead of .0023412093 it returns 2. 
    EDIT:
    Sorry. The question was badly phrased.

    I just copied and pasted that exact code and ran it on my computer, and it's showing me 0.0023412093. Make sure you're running PHP 5 <=, as there could be a bug related to this in earlier versions.
    Last edited by techietim; 09-30-2009 at 07:05 PM.

  9. #8
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Quote Originally Posted by techietim View Post
    I just copied and pasted that exact code and ran it on my computer, and it's showing me 0.0023412093. Make sure you're running PHP 5 <=, as there could be a bug related to this in earlier versions.
    Bluewalrus' question is poorly worded. I came to the same conclusion you did Tim before realizing that what he probably wants is a way to get the key of the lowest value in the array, not the lowest value.

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
  •