1

this is my code:

$arry = (10,20,30,40,50)
function pairwiseDifference($arry, $n) 
{ 
    $diff = 0; 
    for ($i = 0; $i < $n - 1; $i++) 
    { 
        
        // absolute difference between 
        // consecutive numbers 
        $diff = abs($arry[$i] - $arry[$i + 1]); 
        echo $diff." "; 
    } 
} 
 
// Driver Code  
$n = sizeof($arry);
echo "<br> Percent of commisions are: "; pairwiseDifference($arry,$n);  // this echo 10,10,10,10

So now i wanna make the results of pairwise funcition an array to use in another code, i tried to put:

$arru   = pairwiseDifference($arry,$n)[1]; 
$arru1  = pairwiseDifference($arry,$n)[2]; 
$arru2  = pairwiseDifference($arry,$n)[3]; 
$arru3  = pairwiseDifference($arry,$n)[4]; 
echo $arru;
echo $arru1;
echo $arru2;
echo $arru3;

But are not working.
What im wrong?
Thanks in advance!

GINO GINI
  • 35
  • 6
  • Can you tell what is desired behaviour of pairwiseDifference($arry,$n) is for $arry = (10,20,30,40,50) and $n = 5? – Amir MB Mar 19 '21 at 00:07
  • Yes $n are the lenght, in this case are 5 – GINO GINI Mar 19 '21 at 00:12
  • @Amir MB I need to use single output value of pair wise result in another code. But I don't know how make it work like an array to use. – GINO GINI Mar 19 '21 at 00:14
  • If $n is alway the length of array, why are you passing it to the method as a parameter? – Amir MB Mar 19 '21 at 00:14
  • Can you make an example of your required output of the method? – Amir MB Mar 19 '21 at 00:14
  • I need to have: $arr1 = 10 (first result of pair wise) $arr2 = 10 (second result of pair wise) $arr3 = 10 ( third result of Pair wise) $arr4 = 10 (fourth result of pair wise). And then I can use this value in other code. – GINO GINI Mar 19 '21 at 00:18

1 Answers1

1

An array declaration in PHP needs to either have the array keyword or it can just be declared with square brackets []:

$arry = array(10,20,30,40,50);

or

$arry = [10,20,30,40,50];

You can get the size of an array with the count() method, and there is no need to pass it as a parameter to your function:

function pairwiseDifference($arry) 
{ 
    $n = count($arry);
    // ...
}

Your function doesn't return anything, so the variables $arru, $arru1, ... won't get anything back from the function and will remain empty. In order to return an array of differences and solve your problem you can do this:

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(10,20,30,40,50);
echo "<br> Percent of commisions are: ";

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

$arru   = $diffArray[0];  // array elements start at index 0
$arru1  = $diffArray[1];
$arru2  = $diffArray[2];
$arru3  = $diffArray[3];

echo $arru;    // prints 10
echo $arru1;   // prints 10
echo $arru2;   // prints 10
echo $arru3;   // prints 10
Ivan86
  • 5,695
  • 2
  • 14
  • 30