<?php function condense_duplicates($arrays) { $machine_slot_map = array(); foreach ($arrays as $arr) { $machine_slot_key = "{$arr['MACHINE']}_{$arr['slot']}"; if (array_key_exists($machine_slot_key, $machine_slot_map)) { $lot = $arr['LOT']; if (array_key_exists('Duplicate', $machine_slot_map[$machine_slot_key])) { $curr_duplicates = $machine_slot_map[$machine_slot_key]['Duplicate']; $machine_slot_map[$machine_slot_key]['Duplicate'] = "{$curr_duplicates}, {$lot}"; } else { $machine_slot_map[$machine_slot_key]['Duplicate'] = $lot; } } else { $machine_slot_map[$machine_slot_key] = $arr; } } return array_values($machine_slot_map); } $arrays = array ( array('MACHINE' => 'A1', 'LOT' => 'B1077', 'slot' => 1), array('MACHINE' => 'A2', 'LOT' => 'B0229', 'slot' => 2), array('MACHINE' => 'A2', 'LOT' => 'B0132', 'slot' => 2), array('MACHINE' => 'A2', 'LOT' => 'B3967', 'slot' => 2), array('MACHINE' => 'A3', 'LOT' => 'B2644', 'slot' => 3) ); // echo 'Before:', PHP_EOL; // print_r($arrays); $arrays_with_condensed_duplicates = condense_duplicates($arrays); // echo 'After:', PHP_EOL; print_r($arrays_with_condensed_duplicates); ?>
You have javascript disabled. You will not be able to edit any code.