Switch case replace old value with new value

A

Anonymous

Guest
As you can see on response, the first key "1000000020", trail array already have an items. The next key "1000000021" only have the new value. I want to carry the items from "1000000020", and place it for next key. Which means, the items will always be there, even though the next key "1000000022" will either have new key/values or replace new key/values.

This is my current output on postman:
Code:
{
    "status": true,
    "code": 200,
    "timestamp": 1616053150,
    "data": {
        "1000000020": {
            "trail": {
                "customerName": "Mohammed Ali",
                "customerIdType": "New ID",
                "customerIdNumber": "871230ABC",
                "cartId": null,
                "orderStatus": "NEW",
                "amount": "0"
            },
            "authorName": "Daniel",
            "createdDate": "02/02/2021",
            "createdTime": "06:21 PM",
            "submittedDate": "02/02/2021",
            "submittedTime": "06:21 PM",
            "orderType": "New Install"
        },
        "1000000021": {
            "authorName": "uagent0001",
            "createdDate": "02/25/2021",
            "createdTime": "06:21 PM",
            "submittedDate": "02/25/2021",
            "submittedTime": "06:21 PM",
            "orderType": "New Install",
            "trail": {
                "refNo": "1000000008"  <- *new value added*
            }
        }
    }
}

This is my code:
Code:
$orderTrail = $newValues = [];
$savedBatchId = null;
$batches = $trail;

/** loop through for the batch Id */
foreach ($batches as $b) {   
     if ($b->batch_id != $savedBatchId) {
        /** Save the new batchId read */
        $savedBatchId = $b->batch_id;

        /** 
        * If it's a new batch id, create a loop to get the
        * key new value pair
        */
         foreach($trail as $t) {
              if ($t->batch_id == $b->batch_id) {
                  switch ($t->key) {
                       case 'customer_id': 
                            $customer = $this->customer->findById((int)$t->new_value);
                            $orderTrail[$b->batch_id]['trail']['customer_name'] = $customer->full_name;
                            $orderTrail[$b->batch_id]['trail']['customer_id_type'] = $customer->id_type;
                            $orderTrail[$b->batch_id]['trail']['customer_id_number'] = $customer->id_number;
                       break;

                       case 'cart_id':
                             $cart = $this->cart->findById((int)$t->new_value);
                             $orderTrail[$b->batch_id]['trail'][$t->key] = $cart->cart_type;
                       break;

                       default:
                             $orderTrail[$b->batch_id]['trail'][$t->key] = $t->new_value;
                       break;
                  }
               }
               $orderTrail[$b->batch_id]['author_name'] = $t->user->name;
               $orderTrail[$b->batch_id]['created_date'] = Carbon::parse($t->created_at)->format('m/d/Y');
               $orderTrail[$b->batch_id]['created_time'] = Carbon::parse($t->created_at)->format('h:i A');
               $orderTrail[$b->batch_id]['submitted_date'] = Carbon::parse($t->created_at)->format('m/d/Y');
               $orderTrail[$b->batch_id]['submitted_time'] = Carbon::parse($t->created_at)->format('h:i A');
               $orderTrail[$b->batch_id]['order_type'] = 'New Install';
         }
    }
}
return $orderTrail;

I want to have expected output like this one:
Code:
{
    "status": true,
    "code": 200,
    "timestamp": 1616053150,
    "data": {
        "1000000020": {
            "trail": {
                "customerName": "Mohammed Ali",
                "customerIdType": "New ID",
                "customerIdNumber": "871230ABC",
                "cartId": null,
                "orderStatus": "NEW",
                "amount": "0"
            },
            "authorName": "Daniel",
            "createdDate": "02/02/2021",
            "createdTime": "06:21 PM",
            "submittedDate": "02/02/2021",
            "submittedTime": "06:21 PM",
            "orderType": "New Install"
        },
        "1000000021": {
            "authorName": "Daniel",
            "createdDate": "02/25/2021",
            "createdTime": "06:21 PM",
            "submittedDate": "02/25/2021",
            "submittedTime": "06:21 PM",
            "orderType": "New Install",
            "trail": {
                "customerName": "Mohammed Ali", <-- carry old value from above key
                "customerIdType": "New ID",
                "customerIdNumber": "871230ABC",
                "cartId": null,
                "orderStatus": "NEW",
                "amount": "0"
                "refNo": "1000000008"  <-- new value
            }
        },
        "1000000022": {
            "authorName": "Daniel",
            "createdDate": "02/27/2021",
            "createdTime": "07:10 PM",
            "submittedDate": "02/27/2021",
            "submittedTime": "07:10 PM",
            "orderType": "New Install",
            "trail": {
                "customerName": "Mohammed Ali", <-- carry old value from above key
                "customerIdType": "New ID",
                "customerIdNumber": "871230ABC",
                "cartId": null,
                "orderStatus": "EXPIRED",  <-- replace value
                "amount": "0"
                "refNo": "1000000008"  <-- carry from above key
            }
        },
    }
}

I have tried to create a new array to stored new value, but it didn't work. And I'm not sure what is wrong with that code. Anyone has an idea for this? Thank you for your kindness.
 
Where do these new values for each trail (for each data set) come from?
 
Back
Top