2014-01-30 57 views
0

如何獲取數組數據到foreach循環在PHP如何讓PHP

時遇到的代碼如下

$years = $this->report_model->get_year(); 
      print_r($years); 

時上午印刷正在逐漸爲

數組數據到foreach循環
Array ([0] => stdClass Object ([financial_year] => 1972-1973) [1] => stdClass Object ([financial_year] => 1973-1974) [2] => stdClass Object ([financial_year] => 1974-1975) [3] => stdClass Object ([financial_year] => 1975-1976) [4] => stdClass Object ([financial_year] => 1976-1977) [5] => stdClass Object ([financial_year] => 1977-1978) [6] => stdClass Object ([financial_year] => 1978-1979) 

,當我在foreach循環是給作爲

  foreach ($years as $year) { 
      $data['result_data'][] = $this->report_model->get_historical_data($year); 
     } 

我得到的php誤差

A PHP Error was encountered 

Severity: 4096 

Message: Object of class stdClass could not be converted to string 

Filename: database/DB_active_rec.php 

Line Number: 42 

究竟是什麼錯誤我在這裏做

+0

您正在使用某種形式的框架或第三方庫?數據訪問如何配置? – GordonM

回答

1

更改此$this->report_model->get_historical_data($year);

對此

$this->report_model->get_historical_data($year->financial_year);

+0

非常感謝oleksii – user3243029

+0

您能否將我的答案標記爲已接受? –

+0

喜時遇到代碼彷彿((計數($年)> = 1)&& $濾波器){ \t \t \t的foreach($年$年){ \t \t \t \t $數據[ 'result_data'] [] = $ this-> report_model-> get_historical_data($ year); \t \t \t} \t \t \t // print_r($ data); \t \t} \t \t其他{ \t \t \t的foreach($年$年){ \t \t \t \t $數據[ 'result_data'] [] = $這個 - > report_model-> get_historical_data($年 - > financial_year); \t \t \t} \t \t \t // print_r($ data); \t \t} \t \t 的foreach($數據[ 'result_data']爲$鍵=> $的getValue){ 回波$的getValue [0] [0] - >狀態; }但我得到php錯誤 – user3243029

0

試試這個

foreach ($years as $year) { 
      $data['result_data'][] = $this->report_model->get_historical_data($year->financial_year); 
     } 
0

只有集合是陣列

的一部分嘗試是這樣的:

foreach (array $years as $key => $object) { 
    echo $object->financial_year; 
}