![]() |
<p>Message: Cannot use object of type stdClass as array</p> - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: <p>Message: Cannot use object of type stdClass as array</p> (/showthread.php?tid=78041) |
<p>Message: Cannot use object of type stdClass as array</p> - Knutsford - 11-23-2020 Code: I have this in the view Code: SELECT * Code: FROM Users Code: WHERE Type = 'Licensee' Code: AND Status = 'Active' Code: ORDER BY FranchiseeID"; Code: $menus["Licensees"]=array(); Code: $prev_id = 0; Code: $total_licensees_elements = 0; Code: if($result=$this->db->query($query)){ Code: if($result->num_rows() > 0) { Code: while ($row = $result->unbuffered_row()){ Code: if($row->FranchiseeID <> $prev_id){ Code: $total_licensees_elements++; Code: } Code: $row->PreviousFranchiseeID = $prev_id; Code: $menus["Licensees"][]=$row; Code: $prev_id = $row->FranchiseeID; Code: } Code: } Code: } RE: <p>Message: Cannot use object of type stdClass as array</p> - InsiteFX - 11-23-2020 Because you are trying to assign an object to an array. I use these to helper when I need to convert one to the other. PHP Code: // ----------------------------------------------------------------------- Place these in one of your helpers and load it. RE: <p>Message: Cannot use object of type stdClass as array</p> - Knutsford - 11-23-2020 (11-23-2020, 12:53 PM)InsiteFX Wrote: Because you are trying to assign an object to an array. Yep I know ![]() So I jsut need to wrap objectToArray() around $row then when I assign it and it will work? That was where I was stuck. Thanks RE: <p>Message: Cannot use object of type stdClass as array</p> - Knutsford - 11-24-2020 I have changed it to $menus["Licensees"][]=objectToArray($row); But I am now getting Message: Argument 1 passed to objectToArray() must be an object, string given? RE: <p>Message: Cannot use object of type stdClass as array</p> - InsiteFX - 11-24-2020 Your using twig templates so not really sure how twig is doing it but the foreach is nothing like CodeIgniters. you can use this helper to see what your object is looking like, if it's a CodeIgniter database returned object then it will be an object in an object. PHP Code: <?php Pass in your object that is giving you an error and this will give you a formatted output of it. |