![]() |
Query a associative array from Database CI4 - 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: Query a associative array from Database CI4 (/showthread.php?tid=81259) |
Query a associative array from Database CI4 - samaidha - 02-10-2022 Hi, hope you guys are doing ok, I'm having trouble changing an array to an object in CI4, I have my working code in PHP which works fine, but I'm having difficulty writing it on CI4, below is my query for a "search" in my Database that retrieves a result and set it as a associative array(object) with a null value, PHP Code: if (isset($_GET['q'])) { However in CI4. the closest thing I could muster up is below, which kind of work, but don't think it is right Code: $user = new UserModel(); How can I fix this code to get results as the raw PHP, which outputs the whole array converted to an object with its key(from array index) and value as null RE: Query a associative array from Database CI4 - InsiteFX - 02-11-2022 You can place these in a helper and then use to convert. PHP Code: // ----------------------------------------------------------------------- RE: Query a associative array from Database CI4 - iRedds - 02-11-2022 return $this->response->setJSON(array_fill_keys($data, null)); RE: Query a associative array from Database CI4 - samaidha - 02-11-2022 Thank you Both @InsiteFX and @iRedds for taking the time to answer the question, The answer by @iRedds has solved the the problem (greatly appriciated), and thanks to @InsiteFX for providing the helper function. |