Instead of foreach loop, try this (only when your arrays have same length).
$number = COUNT($_POST["codes "]);//count how many arrays available
if($number > 0)
{
for($i=0; $i<$number; $i++)//loop thru each arrays
{
$codes =$_POST['codes'][$i];
$names =$_POST['names'][$i];
//ur code in here
}
}