0

I have an array and I iterate through its first 7 values with this function:

function clicksLast7Days($data)
        {
            $path = $data->data->data->clicks;
            $num_loops = 0;
            foreach ($path as $key => $item){
                $num_loops++;
                if($num_loops > 7) break;
                if ($key >= 0) {
                $array[] = $item->clicks;
                }     
            }
           return json_encode($array);   
        }

The array's structure (if it helps) is:

["data"]=>
  object(stdClass)#212 (3) {
    ["status_code"]=>
    int(200)
    ["data"]=>
    object(stdClass)#211 (3) {
      ["days"]=>
      int(30)
      ["total_clicks"]=>
      int(6)
      ["clicks"]=>
      array(30) {
        [0]=>
        object(stdClass)#215 (2) {
          ["clicks"]=>
          int(0)
          ["day_start"]=>
          int(1466395200)
        }
        [1]=>
        object(stdClass)#216 (2) {
          ["clicks"]=>
          int(0)
          ["day_start"]=>
          int(1466308800)
        }
        [2]=>
        object(stdClass)#217 (2) {
          ["clicks"]=>
          int(0)
          ["day_start"]=>
          int(1466222400)
        }
        [3]=>
        object(stdClass)#218 (2) {
          ["clicks"]=>
          int(0)
          ["day_start"]=>
          int(1466136000)
        }
        [4]=>
        object(stdClass)#219 (2) {
          ["clicks"]=>
          int(0)
          ["day_start"]=>
          int(1466049600)
        }
        [5]=>
        object(stdClass)#220 (2) {
          ["clicks"]=>
          int(0)
          ["day_start"]=>
          int(1465963200)
        }
        [6]=>
        object(stdClass)#221 (2) {
          ["clicks"]=>
          int(0)
          ["day_start"]=>
          int(1465876800)
        }
        [7]=>
        object(stdClass)#222 (2) {
          ["clicks"]=>
          int(0)
          ["day_start"]=>
          int(1465790400)
        }

The problem is that by using my function I get an array of elements: [0,0,0,0,0,0,0]. But what I want is to obtain their sum (which in this case is 0), and not an array, just a number.

3
  • 4
    before loop $sum = 0; in loop $sum += $item->clicks; Commented Jun 22, 2016 at 10:31
  • If you want to calculate the sum, then why putting the values in an array? Why not using +=? Commented Jun 22, 2016 at 10:31
  • thank you splash58, this helped me. :) Commented Jun 22, 2016 at 10:34

1 Answer 1

1
$clicksArray = $data->data->data->clicks;
$sum = array_sum(
    array_map(
        $clicksArray, 
        function($clickElement) {
            return $clickElement->clicks;
        }
    )
);

What does this mean ?

  • array_sum will sum all values of a certain array. More information here

  • array_map will return all elements of an array, after applying a function (so, basically here, we are returning only clicks attribute

Sign up to request clarification or add additional context in comments.

1 Comment

array_reduce would serve here well, too

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.