Reputation: 3
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
Upvotes: 0
Views: 759
Reputation: 505
Only the collection is part of the array
Try something like this:
foreach (array $years as $key => $object) {
echo $object->financial_year;
}
Upvotes: 0
Reputation: 257
try this
foreach ($years as $year) {
$data['result_data'][] = $this->report_model->get_historical_data($year->financial_year);
}
Upvotes: 0
Reputation: 1106
Change this $this->report_model->get_historical_data($year);
To this
$this->report_model->get_historical_data($year->financial_year);
Upvotes: 1