I’m looping through a mysql query and storing the values to an object. I want to automatically iterate the object like one would an array as I store the data to it.
Here is what I’m doing:
$i = 0;
foreach( $query->result() as $row )
{
$data->$i = $row;
$i++;
}
I’d like to mimic the code below, but for an object, removing the need for $i in the above code:
foreach( $query->result() as $row )
$data[] = $row;
What is the equivalent to $data[] = $row to iterate an object as you store variables to it in a foreach or while loop?
NOTE: It’s clear that I shouldn’t be using objects in this manner. Could you please elaborate on why this is the case?
There isn’t one, because objects don’t work that way. Besides, if you want to store things in a linear fashion then you shouldn’t be using an object in that manner regardless.