Switch case replace old value with new value

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
User avatar
jessie25
New php-forum User
New php-forum User
Posts: 1
Joined: Thu Mar 18, 2021 12:51 am

Thu Mar 18, 2021 1:05 am

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: Select all

{
    "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: Select all

$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: Select all

{
    "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.
iago
php-forum Active User
php-forum Active User
Posts: 41
Joined: Thu Mar 26, 2020 2:33 pm

Sun Mar 21, 2021 3:07 pm

Where do these new values for each trail (for each data set) come from?
Post Reply