skip to Main Content

i have this function:

function pairwiseDifference($arry) 
{
    $n = count($arry) - 1;  // you can add the -1 here
    $diff = 0;
    $result = array();

    for ($i = 0; $i < $n; $i++) 
   { 
       // absolute difference between 
       // consecutive numbers 
       $diff = abs($arry[$i] - $arry[$i + 1]); 
       echo $diff." ";
       array_push($result, $diff);   // use array_push() to add new items to an array
   }

   return $result;  // return the result array
}

$arry = array(20,10,10,50);
echo "<br> Percent of commisions are: ";

// this echos 10,0,40 and assigns whatever your function returns 
// to the variable $diffArray
$diffArray = pairwiseDifference($arry);

The problem are that im not expecting this output
becouse first number of array (20) is my commission
and the other numbers are my parents commission (10,10,50).
So basically i need to output like this: (0,0,30)
becouse i take 20 of commission,
first parent not take nothing becouse are less of my commission (10)
second parent not take nothing becouse are less of my commission (10)
and only last parent take 30 becouse are greater than my commission (50 – 20 my commission).
Thanks in advance

2

Answers


  1. To tweak the logic according to your code, there would be only 3 modifications.

    • Create a $max variable and assign it the value of $arry[0].
    • Make difference as 0 if current max is greater than current one, else take the difference.
    • Calculate the new max again using max() function.

    Snippet:

    <?php
    
    function pairwiseDifference($arry) 
    {
        $n = count($arry) - 1;  // you can add the -1 here
        $diff = 0;
        $result = array();
        $max = $arry[0]; // new addition
        for ($i = 1; $i <= $n; $i++)  // new modification <= n instead of < n
       { 
           // absolute difference between 
           // consecutive numbers 
           $diff = $max < $arry[$i] ? $arry[$i] - $max : 0; // new modification
           $max = max($max, $arry[$i]); // new modification
           echo $diff." ";
           array_push($result, $diff);   // use array_push() to add new items to an array
       }
    
       return $result;  // return the result array
    }
    
    $arry = array(20,10,10,50);
    echo "<br> Percent of commisions are: ";
    
    // this echos 10,0,40 and assigns whatever your function returns 
    // to the variable $diffArray
    $diffArray = pairwiseDifference($arry);
    
    Login or Signup to reply.
  2. Since your first element of the array is your commission and the others are the the commissions of parents, and since it seems that you don’t want to include your commission in the result array, you can do something like this:

    function pairwiseDifference($arry) 
    {
        $n = count($arry);
        $diff = 0;
        $result = array();
    
        $myComm = $arry[0];  // your commision
    
        for ($i = 1; $i < $n; $i++) 
        {
            $diff = 0;                     // set $diff to 0 as default
            if($myComm < $arry[$i])          // if your commision < parent commision
                $diff = $arry[$i] - $myComm;
            echo $diff." ";
            array_push($result, $diff);
        }
    
        return $result;
    }
    
    $arry = array(20,10,10,50);
    echo "<br> Percent of commisions are: ";
    
    $diffArray = pairwiseDifference($arry);
    
    echo $diffArray[0];  // prints 0
    echo $diffArray[1];  // prints 0
    echo $diffArray[2];  // prinst 30
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search