简体   繁体   中英

how to get the array data into foreach loop in php

how to get the array data into foreach loop in php

am having the code as follows

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

when am printing am getting as

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 )

when i was giving in foreach loop as

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

am getting php error as

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

what was the mistake am doing here

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

To this

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

try this

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

Only the collection is part of the array

Try something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM