I am trying to read through an array in the same way I do many other times on my site. This one is giving me the Fatal error: Cannot use object of type stdClass as array. In my class I have:
public $productlist = array();
//GetFeatured
while($obj = $result->fetch_object()){
$this->productlist[] = $obj;
}
In my page I am trying to display it:
$prod = new product;
$prod->GetFeatured();
for($ind = 0; $ind <= count($prod->productlist) - 1; $ind++){
echo $prod->productlist[$ind]['productid'];
}
This populates the array but I am not sure why I am getting this error. Any help is greatly appreciated.
You are fetching as object here:
while($obj = $result->fetch_object()){
then trying to access as array here:
echo $prod->productlist[$ind]['productid'];
Try it like this:
echo $prod->productlist[$ind]->productid;
Another way is using fetch_assoc
at the place of fetch_object
while($obj = $result->fetch_assoc()){
then this stays like this:
echo $prod->productlist[$ind]['productid'];
If you like to get data from object in array style your class needs to implement the ArrayAccess interface