我有這個模型(爲了清楚起見,我只貼出相關的部分我的問題):陣推越來越僅有1對象笨PHP
public function getMissedEntryesForInterval($from_date, $to_date){
$missedEntryObject = null;
if(isset($from_date, $to_date)){
$missedEntryObject = new MissedInsertions_model;
$missedEntryObject->entryId=0;
$missedEntryObject->entryWeight=0;
$missedEntryObject->entryEventType=0;
$missedEntryObject->entryInsertBy=0;
$missedEntryObject->entryModifiedDate=0;
foreach ($finallist as $inputElement) {
if (5000 <= (int)$inputElement->weight) {
$this->_agregateRowToEntry($missedEntryObject, $inputElement);
}
}
return $missedEntryObject;
}
private function _agregateRowToEntry($entryObject, $input){
$entryObject->entryId = $input->id;
$entryObject->entryWeight = $input->weight;
$entryObject->entryEventType = $input->etid;
$entryObject->entryInsertBy = $input->insert_by;
$entryObject->entryModifiedDate = $input->modified_date;
}
public function jsonSerialize() {
return [
'entry_id' => $this->entryId,
'entry_weight' => $this->entryWeight,
'entry_event' => $this->entryEventType,
'entry_insert_by' => $this->entryInsertBy,
'entry_modified_date' => $this->entryModifiedDate
];
}
我的$ finallist(我的開始時間結束在郵遞員時間)看起來像這樣:
[{"id":"58560","weight":"2160","added_date":"2016-08-14 09:01:58","etid":"3","insert_by":"1","modified_date":"2017-04-04 12:26:05"},{"id":"58588","weight":"4300","added_date":"2016-08-14 09:23:23","etid":"3","insert_by":"1","modified_date":"2017-04-04 12:26:05"},{"id":"58862","weight":"40800","added_date":"2016-08-14 15:24:21","etid":"3","insert_by":"1","modified_date":"2017-04-04 12:26:05"},{"id":"59521","weight":"38780","added_date":"2016-08-15 11:42:06","etid":"3","insert_by":"1","modified_date":"2017-04-04 12:26:05"},{"id":"60176","weight":"45260","added_date":"2016-08-15 19:48:20","etid":"3","insert_by":"1","modified_date":"2017-04-04 12:26:05"},{"id":"60389","weight":"38780","added_date":"2016-08-16 11:18:44","etid":"3","insert_by":"1","modified_date":"2017-04-04 12:26:05"},{"id":"61730","weight":"42380","added_date":"2016-08-16 19:15:22","etid":"3","insert_by":"1","modified_date":"2017-04-04 12:26:05"},{"id":"62369","weight":"42800","added_date":"2016-08-17 15:25:46","etid":"3","insert_by":"1","modified_date":"2017-04-04 12:26:05"}]
在我控制我把這種喜歡:
public function getMissedBalanceEventsNew() {
$from_date = $this->input->post('startDate');
$to_date = $this->input->post('endDate');
$result[] = $this->MissedInsertions_model->getMissedEntryesForInterval($from_date,$to_date);
echo json_encode($result);
}
但是當我調用控制器的模式,我只得到這個對象:
[{"entry_id":"62369","entry_weight":"42800","entry_event":"3","entry_insert_by":"1","entry_modified_date":"2017-04-04 12:26:05"}]
希望是這樣的:
[{"entry_id":"58862","weight":"40800","entry_event":"3","entry_insert_by":"1","entry_modified_date":"2017-04-04 12:26:05"},{"entry_id":"58862","entry_weight":"40800","entry_event":"3","entry_insert_by":"1","entry_modified_date":"2017-04-04 12:26:05"},{"entry_id":"59521","entry_weight":"38780","entry_insert_by":"1","entry_modified_date":"2017-04-04 12:26:05"},{"entry_id":"60176","entry_weight":"45260","entry_event":"3","entry_insert_by":"1","entry_modified_date":"2017-04-04 12:26:05"},{"id":"60389","entry_weight":"38780","entry_event":"3","entry_insert_by":"1","entry_modified_date":"2017-04-04 12:26:05"},{"entry_id":"61730","entry_weight":"42380","entry_event":"3","entry_insert_by":"1","entry_modified_date":"2017-04-04 12:26:05"},{"entry_id":"62369","entry_weight":"42800","entry_event":"3","entry_insert_by":"1","entry_modified_date":"2017-04-04 12:26:05"}]
基本上我想從有體重> 5000 $ finallist所有對象在我的控制器列表中。我認爲我必須在控制器中做一些循環,但究竟是什麼讓我的列表不會被1個對象取代?謝謝!
我有我的模式,即迭代列表中的foreach,但我的問題是,在控制器列表我會得到最後的對象,(對象只是替換,我保持最後一個)。 –
我看到了,這就是爲什麼我刪除了評論。 ;-)一個問題,這不是所有的代碼,對吧?我的意思是:'foreach($ finallist as $ inputElement)'不應該工作,因爲你從不定義'$ finallist'變量? –
不,我並沒有把所有的業務邏輯都與我的問題無關。 –