Welcome Guest, Not a member yet? Register   Sign In
Retrieving Basic Arrays of Data
#1

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')
                                  ->findAll(); 
Which produces:
Code:
   id  name_main
0 6595 Marcs Test Yard
1 3886 CATTLE CO.
2 2074 LIVESTOCK MARKET, INC.
3 3949 RIVER RANCH

But what I want is actually:
Code:
id  name_main
6595 Marcs Test Yard
3886 CATTLE CO.
2074 LIVESTOCK MARKET, INC.
3949 RIVER RANCH

So I end up converting the first one with this:
PHP Code:
foreach($peopleTemp as $onePerson) {
    $people[$onePerson['id']] = $onePerson['name_main'];
 } 

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?
Reply
#2

> 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');
Reply
#3

(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?

I don't know.

$people = array_column($peopleTemp, 'name_main', 'id');

Thanks. That is simpler than the foreach! Still, it seems like something that should be built into CI.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB