How does the reassignment of $crm_record array in each loop iteration affect the final output of the code?

The reassignment of the $crm_record array in each loop iteration overwrites the previous values, causing only the last iteration's values to be stored in the array. To solve this issue, you can create a new array for each iteration and store it in a larger array to keep track of all the values.

$crm_records = [];

foreach ($data as $row) {
    $crm_record = [];
    
    // Process $row and populate $crm_record
    
    $crm_records[] = $crm_record;
}

// $crm_records now contains all the $crm_record arrays from each iteration