How to prevent duplication of object inside by pushing it to the array in php?

Viewed 139

I have question, is it possible not to duplicate the array object by looping on it? Right now I used laravel as my backend

I have here my response which is the exchange object duplicate itself.

 [
    {
        "exchange": {
            "id": 1,
            "branch": "BB1",
            "old_check_no": "0001",
            "cash": "250000",
            "bank_deposit": "1000000",
            "offset": "250000",
            "amount": "10000",
            "over_under": null,
            "checkDate": "2021-09-11",
            "remarks": "1",
            "date_closed": "2021-09-11"
        },
        "exchange_list": {
            "exchange_id": 1,
            "new_check_no": "001",
            "new_check_bank": "bank",
            "new_check_branch": "Lagros"
        }
    },
    {
        "exchange": {
            "id": 1,
            "branch": "BB1",
            "old_check_no": "0001",
            "cash": "250000",
            "bank_deposit": "1000000",
            "offset": "250000",
            "amount": "10000",
            "over_under": null,
            "checkDate": "2021-09-11",
            "remarks": "1",
            "date_closed": "2021-09-11"
        },
        "exchange_list": {
            "exchange_id": 1,
            "new_check_no": "002",
            "new_check_bank": "bank",
            "new_check_branch": "Lagros"
        }
    },
]

Now my goal is to push the exchange without duplication:

 [
    {
        "exchange": {
            "id": 1,
            "branch": "BB1",
            "old_check_no": "0001",
            "cash": "250000",
            "bank_deposit": "1000000",
            "offset": "250000",
            "amount": "10000",
            "over_under": null,
            "checkDate": "2021-09-11",
            "remarks": "1",
            "date_closed": "2021-09-11"
        },
        "exchange_list": {
            "exchange_id": 1,
            "new_check_no": "001",
            "new_check_bank": "bank",
            "new_check_branch": "Lagros"
        },
        "exchange_list": {
            "exchange_id": 1,
            "new_check_no": "002",
            "new_check_bank": "bank",
            "new_check_branch": "Lagros"
        }
    }
]

Here is what my foreach loop like and how i push the array object.

$myArray = [];
    foreach($exchange_check as $primary_array) {
       foreach($exchange_lists as $second_array) {
           if($second_array->exchange_id == $primary_array->id) {
                array_push($myArray, (object)[
                    'exchange' => $primary_array,
                    'exchange_list' => $second_array,
                ]);
           }
       }
    }

Thanks

1 Answers

You should add exchange data to array only once in first loop:

$myArray = [];
foreach($exchange_check as $primary_array) {
     $idx = array_push($myArray, ['exchange' => $primary_array]); 
     foreach($exchange_lists as $second_array) {
         if($second_array->exchange_id == $primary_array->id) {
            //array_push() returns the new number of elements in the array, 
            //to get currently added array element we should subtract 1 from this number 
            $myArray[$idx-1]['exchange_list'][] = $second_array;
         }
     }
}

And in second loop add only exchange_list data to array.

Related