0

How can i add array_sum to the string in my loop without making another foreach loop for it? I am trying to combine all of the numbers together instead of having this multi dimensional array and then just have the value and i see that array_sum wont add them up because its inside of an array. any ideas?

$hours_arr = array();
foreach($proj_time as $item){
    $hours_arr [$item['project_id']]['item_value'] = $item['item_value'];
    $hours_arr [$item['project_id']]['hours'][] = $item['hours'];
}

//output
array (size=3)
  4 => 
    array (size=2)
      'item_value' => string 'Coaching' (length=8)
      'hours' => 
        array (size=1)
          0 => string '999.99' (length=6)
  1487 => 
    array (size=2)
      'item_value' => string 'Standby' (length=7)
      'hours' => 
        array (size=1)
          0 => string '15.00' (length=5)
  1488 => 
    array (size=2)
      'item_value' => string 'Standby' (length=7)
      'hours' => 
        array (size=4)
          0 => string '10.00' (length=5)
          1 => string '10.00' (length=5)
          2 => string '10.00' (length=5)
          3 => string '10.00' (length=5)

I would like my output to be

1488 => 
    array (size=2)
      'item_value' => string 'Standby' (length=7)
      'hours' => string '40.00' (length=5)

edit: added contents of $proj_time

Array
(
    [0] => Array
        (
            [project_id] => 4
            [consultant_id] => 51
            [engagement_id] => 8
            [hours] => 999.99
            [item_value] => Coaching
        )

    [1] => Array
        (
            [project_id] => 1487
            [consultant_id] => 1
            [engagement_id] => 1
            [hours] => 15.00
            [item_value] => Standby
        )

    [2] => Array
        (
            [project_id] => 1488
            [consultant_id] => 31
            [engagement_id] => 7
            [hours] => 10.00
            [item_value] => Design App RFP
        )

    [3] => Array
        (
            [project_id] => 1488
            [consultant_id] => 32
            [engagement_id] => 41
            [hours] => 10.00
            [item_value] => Training
        )

    [4] => Array
        (
            [project_id] => 1488
            [consultant_id] => 55
            [engagement_id] => 41
            [hours] => 10.00
            [item_value] => Training
        )

    [5] => Array
        (
            [project_id] => 1488
            [consultant_id] => 1
            [engagement_id] => 1
            [hours] => 10.00
            [item_value] => Standby
        )

)
2
  • can you show the $proj_time array contents? Commented Jun 29, 2016 at 9:04
  • @RomanPerekhrest Added it. Commented Jun 29, 2016 at 9:07

2 Answers 2

1

Instead of creating array and then applying operation, while creating itself why don't you sum up like this:

DEMO

$hours_arr = array();
foreach($proj_time as $item){
    $hours_arr [$item['project_id']]['item_value'] = $item['item_value'];
    if(array_key_exists('hours', $hours_arr [$item['project_id']]))
        $hours_arr [$item['project_id']]['hours'] += $item['hours'];
    else
        $hours_arr [$item['project_id']]['hours'] = $item['hours']; 
}

Result:

Array
(
    [4] => Array
        (
            [item_value] => Coaching
            [hours] => 999.99
        )

    [1487] => Array
        (
            [item_value] => Standby
            [hours] => 15
        )

    [1488] => Array
        (
            [item_value] => Standby
            [hours] => 40
        )

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

1 Comment

Thank you, I appreciate it.
0

Try this out

<?php
    $hours_arr = array();
    foreach($proj_time as $item){
        if(!isset($hours_arr [$item['project_id']]) || $hours_arr [$item['project_id']]['item_value'] != $item['item_value']) {     
            $hours_arr [$item['project_id']]['item_value'] = $item['item_value'];
            $hours_arr [$item['project_id']]['hours'][] = $item['hours'];
        } else {
            $hours_arr [$item['project_id']]['hours'][0] += $item['hours'];
        }
    }

2 Comments

No luck, the array is almost still the same. pastebin.com/ci55girE
Oops my bad, forgot the add the key for hours. Try it now

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.