nathpilland
nathpilland

Reputation: 304

How do you access the elements of the array returned from $this->query(select....)?

I am calling the query function from cakephp, and I am selecting two fields from the database. How do I access each field from the array? The query is as such

$fieldKeys = $this->query("select FLOWSHEET_NAME, FLOWSHEET_ID from FLOWSHEET_TEMPLATE");

And I want to do something along the lines of:

foreach($fieldKeys as $value){
        foreach($value as $nest){
            foreach($nest as $id){
                $mapArray[$id[0]] = $id[1];
            }
        }
    }

But this doesn't work. This works but $id[0] only gives back the first letter of the string held in the FLOWSHEET_NAME column, whereas I want the whole string.

Upvotes: 0

Views: 83

Answers (2)

dr Hannibal Lecter
dr Hannibal Lecter

Reputation: 6721

Cake gives you tools to inspect the array and see for yourself. These tools are pr($varname) and debug($varname), but there are other non-cake functions as well.

This is not really a cake question, it's an elementary programming class question.

Upvotes: 1

Your $nest is likely an associative array that you need to loop through like this:

foreach ($nest as $k => $v) { foreach($V as $id) { ... } }

Upvotes: 0

Related Questions