我正在循環foreach,我需要做一些這樣的邏輯: 如果迭代不是最後一個。收集價格。當迭代是最後的時候。從收集的價格中減去總數。除了最後一次迭代價格。我沒有得到下面的代碼。但它不起作用。確定並做foreach循環,除了最後一次迭代
$i = 0;
$credit = '';
$count = count($reslist);
foreach ($reslist as $single_reservation) {
//All of the transactions to be settled by course
//$credit = $this->Reservations_model->find_res_price($single_reservation['value']) * $this->input->post('currency_value');
if ($i > $count && $single_reservation != end($reslist)) {
$gather_sum_in_czk += $this->Reservations_model->find_res_price($single_reservation['value']) * $this->input->post('currency_value');
$credit = $this->Reservations_model->find_res_price($single_reservation['value']) * $this->input->post('currency_value');
}
//Last iteration need to subtract gathered up sum with total.
else {
$credit = $suminczk - $gather_sum_in_czk;
}
$i++;
}
編輯:試圖收集了價格所有互動EXECPT LAST:
if ($i != $count - 1 || $i !== $count - 1) {
$gather_sum_in_czk += $this->Reservations_model->find_res_price($single_reservation['value']) * $this->input->post('currency_value');
$credit = $this->Reservations_model->find_res_price($single_reservation['value']) * $this->input->post('currency_value');
}
else {
$credit = $suminczk - $gather_sum_in_czk;
}
你有一個計數器'$ i'和'count'中的總數,所以'if($ i == $ counter){}'會抓住最後一個嗎? – Egg
在第一個循環中'$ i> $ count',$ i是0,因此它怎麼會比$ count更大? –
看我的編輯。我需要先收集最後所有的答案。然後使用'$ reslist'的最後一個 – Prague2