Php 在数组中使用foreach

Php 在数组中使用foreach,php,codeigniter,Php,Codeigniter,我正试图使用codeigniter的活动记录类从数据库中获取一个值,该值需要用此代码插入到表中 $q = $this->db->get('center_number'); $cd = $this->db->get('running_tasks');` $ask_for_permission = array( 'messagefrom' => $le_message_from, 'messageto' => foreach ($q->re

我正试图使用codeigniter的活动记录类从数据库中获取一个值,该值需要用此代码插入到表中

$q = $this->db->get('center_number');
$cd = $this->db->get('running_tasks');`

$ask_for_permission = array(
    'messagefrom' => $le_message_from,
    'messageto' => foreach ($q->result() as $row)
    {
        $row->center_number;
    },
    'messagetext' => 'The dataset'. '' .foreach ($cd->result() as $row)
    {
        $row->task_name;
    }. ' is requesting permission to credit the account.Reply with yes to allow or no to decline.Anything else other than yes or no shall be ignored.'
);
我得到一个错误:

unexpected 'foreach'
如何从
$ask\u for\u permission
数组中从数据库中获取记录?

试试这个

// First Generate the Array
$ask_for_permission = array(
   'messagefrom' => $le_message_from,
   'messageto' => '',
   'messagetext' => 'The dataset'. '' .foreach ($cd->result() as $row)
                    {
                    $row->task_name;
                    }. ' is requesting permission to credit the account.Reply with yes to allow or no to  
                    decline.Anything else other than yes or no shall be ignored.'
    );

// Second, populate it with all values of the $q->result() array
foreach ($q->result() as $row)
                    {
                    $ask_for_permission['messageto'] .= $row->center_number;
                    }
function msgTo() {
    $str = '' ;
    foreach ($r->result() as $row) {
        $str .= $row;
    }
    return $str;
}
用msgTo()替换您的foreach()

我这样解决了这个问题

    $q = $this->db->get('center_number');

    $le_message_from = '08009898';
    $ask_for_permission = array(
   'messagefrom' => $le_message_from,
   'messageto' => $q->row->center_number,           
   'messagetext' => 'The dataset'. ' '. $this->db->query("select task_name from running_tasks limit 1")->row()->task_name .' '.'is requesting permission to credit the account.Reply with yes to allow or no to  
                    decline.Anything else other than yes or no shall be ignored.'
    );

    $this->db->insert('messageout', $ask_for_permission);

这不是填充数组的方式…根本没有意义您不能在该位置使用
foreach
。可以事先收集数组中的数据,并将其分配给
messageto
键,也可以随后在循环中填充
messageto
。请给出要从活动记录文档中获得的结果示例