Search code examples
phpjsonoperatorssubtotal

Using "+=" operator in PHP


I have a foreach loop that I'm running through JSON data. One of the values in the JSON is a number:

{
  "apiVersion": "0.1",
  "data": {
    "offset": 0,
    "limit": 50,
    "count": 50,
    "total": 783,
    "reports": [
      {
        "type": "ROOM_CHARGE",
        "data": {
          "datetime": "2014-11-10T11:08:08-08:00",
          "originator": "265a",
          "roomNumber": "265",
          "chargeItem": "Premium Services",
          "chargeAmountCents": 495
        }
      },
      {
        "type": "STB_EVENT_TIMED",
        "data": {
          "datetime": "2014-11-10T10:38:20-08:00",
          "originator": "242b",
          "roomNumber": "242",
          "eventItem": "Watched movie 31008",
          "duration": "P0Y0M0DT0H7M50.827S"
        }
      },
      {
        "type": "ROOM_CHARGE",
        "data": {
          "datetime": "2014-11-10T09:35:37-08:00",
          "originator": "540a",
          "roomNumber": "540",
          "chargeItem": "Premium Services",
          "chargeAmountCents": 495
        }
      },

You'll notice above the "ROOM_CHARGE" refers to "chargeAmountCents": 495. I'd like to run my foreach loop and add every number together as such:

Notice below my code with $total. I basically want to add all the room charges together, and then echo out the total at the end. I can format the number myself, I'm just having trouble with the "+=" operator and not sure if it is supposed to be used this way? I keep getting "495" when I echo $total.

<?php foreach($output1['data']['reports'] as $reporting): ?>
      <?php if($reporting['type'] == "ROOM_CHARGE") : ?>
      <tr>
          <td><?php echo 'Room Charge'; ?></td>
          <td><?php echo substr($reporting['data']['datetime'], -26, 10) ?></td>
          <td><?php echo substr($reporting['data']['datetime'], -14, 8) ?></td>
          <td><?php echo ($reporting['data']['roomNumber']) ?>  </td>
          <td><?php echo ($reporting['data']['originator']) ?>  </td>                            
          <td><?php echo ($reporting['data']['chargeItem']) ?></td>
          <td><?php echo number_format(($reporting['data']['chargeAmountCents'])/100, 2, '.', ',') ?></td> 
          <td>-</td>
          <?php $total = ""; ?> 
          <?php $total += ($reporting['data']['chargeAmountCents']) ?>         
      </tr>

Solution

  • Inside your loop, you are doing <?php $total = ""; ?>. So, every time you loop, you are resetting the total.

    You need to set $total (to 0, since it's a number) outside the loop.

    <?php $total = 0; ?> 
    <?php foreach($output1['data']['reports'] as $reporting): ?>
          <?php if($reporting['type'] == "ROOM_CHARGE") : ?>
          <tr>
              <td><?php echo 'Room Charge'; ?></td>
              <td><?php echo substr($reporting['data']['datetime'], -26, 10) ?></td>
              <td><?php echo substr($reporting['data']['datetime'], -14, 8) ?></td>
              <td><?php echo ($reporting['data']['roomNumber']) ?>  </td>
              <td><?php echo ($reporting['data']['originator']) ?>  </td>                            
              <td><?php echo ($reporting['data']['chargeItem']) ?></td>
              <td><?php echo number_format(($reporting['data']['chargeAmountCents'])/100, 2, '.', ',') ?></td> 
              <td>-</td>
              <?php $total += ($reporting['data']['chargeAmountCents']) ?>         
          </tr>