1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Code help

Discussion in 'Development' started by Kabluinc, Feb 10, 2017.

  1. Kabluinc

    Kabluinc Baby Zombie

    Messages:
    129
    HI,

    I need some help with code for finding the highest value of an array of variables.

    Forexample the variables are $this->test[1] $this->test[2] and $this->test[3]

    $this->test[1] = 2

    $this->test[2] = 5

    $this->test[3] = 1

    How can I find the variable that has the highest value?. its obvious that $this->test[2] is the highest but how can we find this out using code?

    Many thanks
     
  2. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Sandertv and Awzaw like this.
  3. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    PHP:
    $intArray = [12345];
    $highest 0;
    foreach(
    $intArray as $key => $value) {
        if(
    $value $highest) {
            
    $highest $value;
        }
    }
    echo 
    $highest// 5
    That's a basic check with a loop, I'm sure there's other ways that I can't recall off the top of my head.
     
    corytortoise likes this.
  4. eDroid

    eDroid Witch

    Messages:
    59
    GitHub:
    edroiid
    PHP:
    $nums = [12345];
    $biggest max($nums);
     
    Sandertv likes this.
  5. Bluplayz

    Bluplayz Spider Jockey

    Messages:
    43
    GitHub:
    bluplayz
    you should use this:

    PHP:
    $this->test = [2,5,1];
    $max max($this->test); //will return 5 because its the highest int in your array :)
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.