Rossitten
Rossitten

Reputation: 1166

How to combine array values?

I am about to give up. I knwo it is dman simple but... Here is what I have:

$stuff =     Array ( [0] => 1 ) Array ( [0] => 0.5 ) Array ( [0] => 0.5 ) 

I need a sum the values (so 1 + 0.5 + 0.5) to a string? what on Earth should I do? is there non-recursive way of doing so (like array_sum()) ? Thank you so much in advance.

Upvotes: 0

Views: 39

Answers (1)

Twisty
Twisty

Reputation: 30893

Not sure if this is what you're looking for. Simple answer, iterate over your array of arrays. When you have completed the calculation, re-cast the variable to String if you like.

<?php
$stuff = array(
    array(1),
    array(0.5),
    array(0.5)
);

$result = 0;
foreach($stuff as $i){
    $result += $i[0];
}

$stringResult = strval($result);
echo is_string($stringResult) . " : " . $stringResult;
// 1 : 2
?>

Upvotes: 1

Related Questions