从关联数组中的每个键中获取一个值

时间:2016-05-14 12:33:35

标签: php arrays codeigniter

我有以下数组

Const

我想要做的就是选择' FROM'数组中每个键的值(在这种情况下是-13,11,11),然后将它们发送到数据库以检索codeigniter中的名称和头像等信息。

更新

我试过以下和它得到了我11,11,13,(后面有一个逗号)。

array(
    'messages' => array(
        0 => array(
            'id' => '3',
            'subject' => 'what did you say last night?',
            'message' => 'your mother wai',
            'from' => '13',
            'to' => '1',
            'from_viewed' => '0',
            'to_viewed' => '0',
            'from_deleted' => '0',
            'to_deleted' => '0',
            'from_vdate' => NULL,
            'to_vdate' => NULL,
            'from_ddate' => NULL,
            'to_ddate' => NULL,
            'created' => '2016-05-14 14:02:12',
        ) ,
        1 => array(
            'id' => '2',
            'subject' => 'this is new',
            'message' => 'hello guy',
            'from' => '11',
            'to' => '1',
            'from_viewed' => '0',
            'to_viewed' => '0',
            'from_deleted' => '0',
            'to_deleted' => '0',
            'from_vdate' => NULL,
            'to_vdate' => NULL,
            'from_ddate' => NULL,
            'to_ddate' => NULL,
            'created' => '2016-05-14 13:59:56',
        ) ,
        2 => array(
            'id' => '1',
            'subject' => 'hello boy.',
            'message' => 'i love this too much . what do you think about making this happen for tomorrow? already then . good bye',
            'from' => '11',
            'to' => '1',
            'from_viewed' => '0',
            'to_viewed' => '0',
            'from_deleted' => '0',
            'to_deleted' => '0',
            'from_vdate' => NULL,
            'to_vdate' => NULL,
            'from_ddate' => NULL,
            'to_ddate' => NULL,
            'created' => '2016-05-14 13:54:02',
        ) ,
    ) ,
)

此外,我不想确定它应该回显多少个值。只要它存在,我希望它回应。

1 个答案:

答案 0 :(得分:2)

你可以使用这个

$t = array(
    'messages' => array()
 )
 /// your array ......

$from = '';
foreach($t['messages'] as $k => $m){
    if($k == 0){
        $from = $m['from'];
    }else{
        $from .= ','.$m['from'];
    }
}
echo $from;

它将输出为13,11,11。但是,如果您需要这些值的数组,那么您可以使用以下

$from = array();
foreach($t['messages'] as $k => $m){
        $from[] = $m['from'];

}
print_r($from);