Mensur
Mensur

Reputation: 475

Rewrite a PHP function with arrays instead

Is there any way I could rewrite this function with an array instead of all these if statements? Could i maybe use some for loop together with an array? How would that look like? Any suggestions of simpler code?

Here is my php function:

function to_next_level($point) {

    /*
    **********************************************************************
    *
    *   This function check how much points user has achievents and how much procent it is until next level
    *
    **********************************************************************
    */

    $firstlevel = "3000";
    $secondlevel = "7000";
    $thirdlevel = "15000";
    $forthlevel = "28000";
    $fifthlevel = "45000";
    $sixthlevel = "80000"; 

    if($point <= $firstlevel) {

        $total = ($point/$firstlevel) * 100;
        $remaining =  round($total);

        //echo number_format($remaining, 0, '.', ' ');
        return $remaining;

    } elseif ($point <= $secondlevel) {

        $total = ($point/$secondlevel) * 100;
        $remaining =  round($total);

        //echo number_format($remaining, 0, '.', ' ');
        return $remaining;
    } elseif ($point <= $thirdlevel) {

        $total = ($point/$thirdlevel) * 100;
        $remaining =  round($total);

        //echo number_format($remaining, 0, '.', ' ');
        return $remaining;
    } elseif ($point <= $forthlevel) {

        $total = ($point/$forthlevel) * 100;
        $remaining =  round($total);

        //echo number_format($remaining, 0, '.', ' ');
        return $remaining;
    } elseif ($point <= $fifthlevel) {

        $total = ($point/$fifthlevel) * 100;
        $remaining =  round($total);

        //echo number_format($remaining, 0, '.', ' ');
        return $remaining;
    } elseif ($point <= $sixthlevel) {

        $total = ($point/$sixthlevel) * 100;
        $remaining =  round($total);

        //echo number_format($remaining, 0, '.', ' ');
        return $remaining;
    }


}

Upvotes: 2

Views: 114

Answers (7)

Romnick Susa
Romnick Susa

Reputation: 1288

How about putting your variable into array and loop it?

  function to_next_level($point) {    
      $data[0] = "3000";
      $data[1] = "7000";
      $data[2] = "15000";
      $data[3] = "28000";
      $data[4] = "45000";
      $data[5] = "80000"; 
      foreach ($data as $key => $value) {
          if($point <= $value) {

          $total = ($point/$value) * 100;
          $remaining =  round($total);

          return $remaining;

    }
}

}

I haven't try it. But it might work for you.

Upvotes: 1

Exploit
Exploit

Reputation: 6386

simple:

<?php
$point = 100;
$remaining = 0;
$data = [
    'firstlevel' => 3000,
    'secondlevel' => 7000,
    'thirdlevel' => 15000,
    'forthlevel' => 28000,
    'fifthlevel' => 45000,
    'sixthlevel' => 80000
];

foreach($data as $item)
{
    if($point <= $item)
    {
        $remaining = round(($point / $item ) * 100); //or return val
    }
}

Upvotes: 1

Wee Zel
Wee Zel

Reputation: 1324

lots of answers to this!!

here is mine using a while loop - single exit point outside the loop:

function to_next_level($point) {
    /*
    **********************************************************************
    *
    *   This function check how much points user has achievements and how much percent it is until next level
    *
    **********************************************************************
    */

    $arr_level = array(3000,15000,28000,45000,80000);
    $remaining = false;
    while (!$remaining and list($key,$level) = each($arr_level)) {
        if ($point <= $level) {
            $total = ($point/$level) * 100;
            $remaining = round($total);
        }
    }
    // will return false if $point is greater than highest value in $arr_level
    return $remaining;
}

Upvotes: 2

arkascha
arkascha

Reputation: 42960

Start using OOP programming style. This is the perfect opportunity since it is a task without much complexity. Create a class that acts as central authority. That class can receive more methods over time. That way your code stays easy to maintain since all those functions are kept inside a class.

<?php

class levelAuthority
{
  public static $thresholds = [ 3000, 7000, 15000, 28000, 45000, 80000 ];

  public static function getDistanceToNextlevel($points)
  {
    foreach (self::$thresholds as $threshold) {
      if ($points <= $threshold) {
        $total = ($points/$threshold) * 100;
        $remaining =  round($total);
        return $remaining;
      }
    }
  }
}

// in the calling scope:
$points = 5000;
echo levelAuthority::getDistanceToNextlevel($points);

Upvotes: 2

Will
Will

Reputation: 24709

What about something like this?

function to_next_level($point)
{
    $levels = array(
        3000,
        7000,
        15000,
        28000,
        45000,
        80000
    );

    foreach ($levels as $level)
    {
        if ($point <= $level)
        {
            $total = ($point / $level) * 100;
            $remaining =  round($total);

            //echo number_format($remaining, 0, '.', ' ');
            return $remaining;

        }
    }
}

The point levels are in order in the array, so [0] is $firstlevel, and so on. You simply iterate through the array and return whenever we reach the condition where $point is <= to the the $level.

Also, since $levels is static, it can be defined outside of the function.

Upvotes: 1

doh-nutz
doh-nutz

Reputation: 320

You could write an additional function, that does the calculations and trigger it from the if/else if/else blocks.

function calculate_remaining($points, $level) {
    $total = ($point/$level) * 100;
    $remaining =  round($total);
    return $remaining;
}

You'd trigger this like:

if($point <= $firstlevel) {
    return $calculate_remaining($point, $firstlevel);
} elseif ($point <= $secondlevel) {
    return $calculate_remaining($point, $secondlevel);
} etc.

Upvotes: 1

arbogastes
arbogastes

Reputation: 1338

Try this:

function to_next_level($point) {

    /*
    **********************************************************************
    *
    *   This function check how much points user has achievents and how much procent it is until next level
    *
    **********************************************************************
    */

    $levelArray = array(3000, 7000, 15000, 28000, 45000, 80000);
    foreach ($levelArray as $level) 
    {
        if ($point <= $level) {
            $total = ($point/$level) * 100;
            $remaining =  round($total);  

            //echo number_format($remaining, 0, '.', ' ');
            return $remaining;            
        }
    }

}

Upvotes: 3

Related Questions