如何从php中的对象数组中获取特定的键值

时间:2015-11-06 12:13:10

标签: php arrays

我有一个如下数组:

Array ( [3] => 
   stdClass Object ( 
         [Course_ID] => php01 
         [Course_Type] => E 
         [Course_Name] => PHP 
         [Service] => 3L 
         [Valid_Start_Date] => 2015-11-05 
         [Valid_End_Date] => 2016-01-31 
         [Duration] => 3 
         [Re_cert_Years] => 0 
         [LMS_Course_ID] => 123 
         [id] => 3
     ) 
        [21] => 
    stdClass Object ( 
        [Course_ID] => php01 
        [Course_Type] => E 
        [Course_Name] => PHP 
        [Service] => 3L 
        [Valid_Start_Date] => 2015-11-05 
        [Valid_End_Date] => 2016-01-31 
        [Duration] => 3 
        [Re_cert_Years] => 0 
        [LMS_Course_ID] => 123 
        [id] => 21 
     ) 
  ) 

我想获得Course_ID的值。

如何从这个数组中获取值?

1 个答案:

答案 0 :(得分:1)

要在PHP中访问Object键,请使用->运算符您是否尝试过以下操作:

foreach($ret as $index=>$obj){
    echo $obj->Course_ID;
}

它将打印2个courseID,因为您的对象数组包含2个对象。如果要打印任何特定索引,请使用:

echo $ret[3]->Course_ID;
echo $ret[21]->Course_ID; //where 3 or 21 is index of array