1

this i my array

$arr = array(1,2,3,4,5);

How to get the result that calculate the value of this expression ((((1-2) -3)-4)-5)?

3
  • You mean like this? : stackoverflow.com/questions/7595255/array-reduce-php Commented Sep 29, 2011 at 11:51
  • You mean you want the integer result of that, or that literal string? Commented Sep 29, 2011 at 11:51
  • See the link @hakre posted in that case. Commented Sep 29, 2011 at 11:56

6 Answers 6

3

2 times the first entry minus the whole sum - looks pretty quick.

echo (2 * reset($arr)) - array_sum($arr);
Sign up to request clarification or add additional context in comments.

1 Comment

Note: The use of reset to get the first element of the array is much better than using $arr[0] because it works with the following: $arr = (-1 => 1, 0 => 2, 1 => 2, 2 => 3, 3 => 4, 4 => 5); $arr = array();
3

Just substract the sum of all elements but the first from the first element:

echo array_shift($arr) - array_sum($arr); # -13

To preserve the array, change the calculation a little:

echo $arr[0]*2 - array_sum($arr);  # -13

And we're glad you didn't ask for eval:

echo eval('return '.implode('-', $arr).';'); # -13

However the best suggestion I can give is that you encapsulate the logic into a class and overload the array with it. So that the object than can provide a method for the calculation (Demo):

class Operands extends ArrayObject
{
    public function operate($sign)
    {
        $sign = max(-1, min(1, $sign.'1'));
        $arr = $this->getArrayCopy ();
        return $arr[0]*(1-$sign) + $sign*array_sum($arr);
    }
    public function __invoke($sign) {
        return $this->operate($sign);
    }
}

$arr = new Operands($arr);

echo $arr('-'), ', ' , $arr('+');

2 Comments

This changes the array, which may not be desired. Not that that's a good or bad thing, since @newbie never stated the array must remain intact.
Added some examples that don't do that and - if the calculation is needed multiple times - another example how to "overload" the array. Was not specified in the question either, but might be helpful.
0
<?php echo eval(implode("-", $arr)); ?>

Comments

0

Try this:

$arr = array(1,2,3,4,5);
// load the first number to subtract
$result = $arr[0];
$i = 1;
foreach($arr as $item)
{
    if ($i !=1) // skip the first one
        $result -= $item;
    $i++;
}
echo $result;

?>

1 Comment

Following your example (1-2-3-4-5) should give -13. Is that what you want?
0

This?

<?php
foreach ($arr as $val)
{
   $calc -= $val;
}

echo $calc;
?>

Comments

0

Like @mOrSa, but more readable:

<?php
$arr = array(1, 2, 3, 4, 5);

// load the first number to subtract
$result = $arr[0];

for ($i = 1; $i < count($arr); $i++)
{        
    $result -= $arr[i];
}
echo $result;    
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.