douxing6434
douxing6434
2015-11-06 12:13

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

已采纳

I have an array like below :

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 
     ) 
  ) 

i want to get the value of Course_ID.

How to get the value from this array ?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • duanqiao9541 duanqiao9541 6年前

    For accessing Object key in PHP use -> operator Have you tried like following:

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

    It will print 2 courseID because your array of object contains 2 object. if you want to print any particular index then use this:

    echo $ret[3]->Course_ID;
    echo $ret[21]->Course_ID; //where 3 or 21 is index of array
    
    点赞 评论 复制链接分享

相关推荐