Reputation: 852
I have an error when I am going to insert to database.
I have this array:
When I print_r($students) its structure is this:
Array ( [0] => stdClass Object ( [lastname] => en [firstname] => estudianten [code] => U0009876 [id_estud] => 5 ) [1] => stdClass Object ( [lastname] => Euno [firstname] => estudiante| [code] => U00020814 [id_estud] => 6 ) )
In my model I have this code:
function insert_register_students($students) {
foreach ($students as $student) {
foreach ($student['dates'] as $key => $value) {
$data = array(
'field1' =>$student['id'],
'field2' => $key,
'field3' => '',
);
$this->db->insert('mytable', $data);
}
}
}
In the model how can I do reference that $students is a stdClass Object? The last code in the model works well for me if $students is an array but now has stdClass Object.
What is my error?
Thanks for your help.
Upvotes: 0
Views: 1132
Reputation: 3000
Actually you have an array of stdObject. On each iteration in the foreach you handle an object. So if you want to access its properties you have to do
'field1' => $student->property,
instead of
'field1' => $student['property']
Upvotes: 1
Reputation: 100175
You could do:
foreach ($students as $student) { echo $student->lastname; //and so on }
Hope it helps
Upvotes: 1