PHP-使用带有数组和stdClass对象的foreach获取名称和值

时间:2018-07-17 07:53:41

标签: php arrays

Array 
( 
    [0] => stdClass Object 
        ( 
            [fullname] => sample student 
            [nickname] => sample 
            [photo_ids] => |453 
            [gender] => MALE 
            [nationality] => KOREAN 
        ) 
    [1] => stdClass Object 
        ( 
            [fullname] => kim namkyun 
            [nickname] => tristan 
            [photo_ids] => |448 
            [gender] => MALE 
            [nationality] => KOREAN 
        ) 
)

我有这个array,我想使用foreach循环来获取名称和值。但不要获取例如fullnamesample student。我得到了01,这是result()返回mysql的结果。

我的尝试

$data->param_students = $this->student_model->getItems(array('status'=>'APPROVED'),array('fullname','nickname','photo_ids','gender','nationality'));
foreach ($data->param_students as $key=>$value) {
    print_r($key);
}

2 个答案:

答案 0 :(得分:3)

在下面的代码段中,您将获得所需的键和值。

foreach ($data->param_students as $value) {
   foreach($value as $k => $v){
       echo $k.' '.$v;
   }
}

答案 1 :(得分:1)

$data->param_students = $this->student_model->getItems(array('status'=>'APPROVED'),array('fullname','nickname','photo_ids','gender','nationality'));
foreach ($data->param_students as $value) {
    print_r($value->fullname);
}