![]() |
Retrieving Basic Arrays of Data - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Retrieving Basic Arrays of Data (/showthread.php?tid=80554) |
Retrieving Basic Arrays of Data - SoccerGuy3 - 11-15-2021 I often have the need for a basic array of the row key/ID and a single column's data (think ID and name). With what I know of CI4, I can do the search and end up with an array of arrays with the top level being the array key which is numerical. I then convert this array pretty simply, but am wondering if there is a better, quicker way to get what I really need. Maybe I am missing something but I just don't see what I need in the docs (or I am not searching for the right phrase/command). For example I need the record ID and the person's name. So I do this: PHP Code: $peopleTemp = $this->mailingModel ->select('id, name_main') Code: id name_main But what I want is actually: Code: id name_main So I end up converting the first one with this: PHP Code: foreach($peopleTemp as $onePerson) { So that in my view I can just do: PHP Code: <?= $people[$record['consigner_id']] ?> Surely there has to be a way to get what I need/want in one step in my controller? RE: Retrieving Basic Arrays of Data - kenjis - 11-15-2021 > Surely there has to be a way to get what I need/want in one step in my controller? I don't know. $people = array_column($peopleTemp, 'name_main', 'id'); RE: Retrieving Basic Arrays of Data - SoccerGuy3 - 11-16-2021 (11-15-2021, 09:46 PM)kenjis Wrote: > Surely there has to be a way to get what I need/want in one step in my controller? Thanks. That is simpler than the foreach! Still, it seems like something that should be built into CI. |