如何在php中将数组数据导入foreach循环

时间:2014-01-30 09:41:30

标签: php arrays foreach

如何将数组数据导入php中的foreach循环

我的代码如下

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

当我正在打印时

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

我在这做什么错误

3 个答案:

答案 0 :(得分:1)

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

到此

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

答案 1 :(得分:0)

试试这个

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

答案 2 :(得分:0)

只有集合是数组的一部分

尝试这样的事情:

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