$keys = array_keys($_POST); $sum = 0; $s = 0; for ($i = 0; $i < $count; $i++) { //echo $keys[$i]."<br>"; if (substr_count($keys[$i], "impressions_number_") > 0) { $index = substr($keys[$i], -1, 1); $data[] = array('impressions_number' => $_POST["impressions_number_" . $index], 'base_waste' => $_POST["base_waste_" . $index], 'step_waste' => $_POST["step_waste_" . $index]); $s = $s + 1; /* */ } } foreach ($data as $res) { $sortData[] = $res['impressions_number']; } array_multisort($sortData, SORT_ASC, $data); for ($i = 0; $i < $s; $i++) { $priceLine = new Paperwasteline(); $priceLine->impressions_number = $data[$i]['impressions_number']; if (!empty($data[$i]['base_waste']) && !is_null($data[$i]['base_waste'])) { $priceLine->base_waste = $data[$i]['base_waste']; } else { $data[$i]['base_waste'] = $sugarbean->calc_price($data[$i]['impressions_number'], $data[$i - 1]['impressions_number'], $sugarbean->step_amount, $data[$i - 1]['step_waste'], $data[$i - 1]['base_waste']); $priceLine->base_waste = $sugarbean->calc_price($data[$i]['impressions_number'], $data[$i - 1]['impressions_number'], $sugarbean->step_amount, $data[$i - 1]['step_waste'], $data[$i - 1]['base_waste']); } $priceLine->step_waste = $data[$i]['step_waste']; $priceLine->paperwaste_id = $return_id; $priceLine->save(); } handleRedirect($return_id, 'Paperwaste'); }