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

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x