questions

Pairwise function different calculation

Rate this post

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

 

✔️Solution:

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

Leave a Reply

Your email address will not be published.

Back to top button