从php对象获取数据


get data from php object

我是php的新手,我不知道如何从对象内部的对象中获取数据,然后是数组,然后是数据,所以我想从php中获取数据。我用过这个

echo "<pre>";
var_dump($res);
echo "</pre>";

获取反对的数据为:

 object(stdClass)#1 (1) {
  ["1"]=>
  object(stdClass)#2 (13) {
    ["childSemCategories"]=>
    array(238) {
      [0]=>
      object(stdClass)#3 (13) {
        ["childSemCategories"]=>
        NULL
        ["createdById"]=>
        int(850)
        ["createdDate"]=>
        string(19) "2011-03-14T17:43:50"
        ["displayName"]=>
        string(27) "Abrasives & Grinding Wheels"
        ["displayNamePath"]=>
        string(37) "System >> Abrasives & Grinding Wheels"
        ["entities"]=>
        NULL
        ["id"]=>
        int(2210)
        ["imagePath"]=>
        string(37) "/images/la/s/c/t/n/ctn9mgug_1pe_0.png"
        ["lastModifiedById"]=>
        int(850)
        ["lastModifiedDate"]=>
        string(19) "2011-11-01T13:39:16"
        ["parentId"]=>
        int(1)
        ["parentSemCategory"]=>
        object(stdClass)#4 (13) {
          ["childSemCategories"]=>
          NULL
          ["createdById"]=>
          int(1)
          ["createdDate"]=>
          string(19) "2011-02-16T18:34:48"
          ["displayName"]=>
          string(6) "System"
          ["displayNamePath"]=>
          string(6) "System"
          ["entities"]=>
          NULL
          ["id"]=>
          int(1)
          ["imagePath"]=>
          string(0) ""
          ["lastModifiedById"]=>
          int(1)
          ["lastModifiedDate"]=>
          string(19) "2011-07-13T14:10:50"
          ["parentId"]=>
          NULL
          ["parentSemCategory"]=>
          NULL
          ["status"]=>
          string(6) "ACTIVE"
        }
        ["status"]=>
        string(6) "ACTIVE"
      }
      [1]=>
      object(stdClass)#5 (13) {
        ["childSemCategories"]=>
        NULL
        ["createdById"]=>
        int(850)
        ["createdDate"]=>
        string(19) "2011-03-14T17:46:06"
        ["displayName"]=>
        string(25) "Acrylic Plastics & Sheets"
        ["displayNamePath"]=>
        string(35) "System >> Acrylic Plastics & Sheets"
        ["entities"]=>
        NULL
        ["id"]=>
        int(2242)
        ["imagePath"]=>
        string(37) "/images/la/s/w/b/n/wbncmgug_1qa_0.png"
        ["lastModifiedById"]=>
        int(850)
        ["lastModifiedDate"]=>
        string(19) "2011-11-01T13:41:36"
        ["parentId"]=>
        int(1)
        ["parentSemCategory"]=>
        object(stdClass)#6 (13) {
          ["childSemCategories"]=>
          NULL
          ["createdById"]=>
          int(1)
          ["createdDate"]=>
          string(19) "2011-02-16T18:34:48"
          ["displayName"]=>
          string(6) "System"
          ["displayNamePath"]=>
          string(6) "System"
          ["entities"]=>
          NULL
          ["id"]=>
          int(1)
          ["imagePath"]=>
          string(0) ""
          ["lastModifiedById"]=>
          int(1)
          ["lastModifiedDate"]=>
          string(19) "2011-07-13T14:10:50"
          ["parentId"]=>
          NULL
          ["parentSemCategory"]=>
          NULL
          ["status"]=>
          string(6) "ACTIVE"
        }
        ["status"]=>
        string(6) "ACTIVE"
      }
      [2]=>
      object(stdClass)#7 (13) {
        ["childSemCategories"]=>
        NULL
        ["createdById"]=>
        int(2644)
        ["createdDate"]=>
        string(19) "2013-01-14T15:30:24"
        ["displayName"]=>
        string(19) "Adhesives & Fillers"
        ["displayNamePath"]=>
        string(29) "System >> Adhesives & Fillers"
        ["entities"]=>
        NULL
        ["id"]=>
        int(10155)
        ["imagePath"]=>
        NULL
        ["lastModifiedById"]=>
        int(2644)
        ["lastModifiedDate"]=>
        string(19) "2013-01-14T15:31:01"
        ["parentId"]=>
        int(1)
        ["parentSemCategory"]=>
        object(stdClass)#8 (13) {
          ["childSemCategories"]=>
          NULL
          ["createdById"]=>
          int(1)
          ["createdDate"]=>
          string(19) "2011-02-16T18:34:48"
          ["displayName"]=>
          string(6) "System"
          ["displayNamePath"]=>
          string(6) "System"
          ["entities"]=>
          NULL
          ["id"]=>
          int(1)
          ["imagePath"]=>
          string(0) ""
          ["lastModifiedById"]=>
          int(1)
          ["lastModifiedDate"]=>
          string(19) "2011-07-13T14:10:50"
          ["parentId"]=>
          NULL
          ["parentSemCategory"]=>
          NULL
          ["status"]=>
          string(6) "ACTIVE"
        }
        ["status"]=>
        string(6) "ACTIVE"
      }
      [3]=>
      object(stdClass)#9 (13) {
        ["childSemCategories"]=>
        NULL
        ["createdById"]=>
        int(1)
        ["createdDate"]=>
        string(19) "2011-02-16T18:34:48"
        ["displayName"]=>
        string(32) "Advertising, Promotions & Events"
        ["displayNamePath"]=>
        string(42) "System >> Advertising, Promotions & Events"
        ["entities"]=>
        NULL
        ["id"]=>
        int(2)
        ["imagePath"]=>
        string(35) "/images/la/s/k/0/4/k04dmgug_2_0.png"
        ["lastModifiedById"]=>
        int(850)
        ["lastModifiedDate"]=>
        string(19) "2011-11-01T13:41:57"
        ["parentId"]=>
        int(1)
        ["parentSemCategory"]=>
        object(stdClass)#10 (13) {
          ["childSemCategories"]=>
          NULL
          ["createdById"]=>
          int(1)
          ["createdDate"]=>
          string(19) "2011-02-16T18:34:48"
          ["displayName"]=>
          string(6) "System"
          ["displayNamePath"]=>
          string(6) "System"
          ["entities"]=>
          NULL
          ["id"]=>
          int(1)
          ["imagePath"]=>
          string(0) ""
          ["lastModifiedById"]=>
          int(1)
          ["lastModifiedDate"]=>
          string(19) "2011-07-13T14:10:50"
          ["parentId"]=>
          NULL
          ["parentSemCategory"]=>
          NULL
          ["status"]=>
          string(6) "ACTIVE"
        }
        ["status"]=>
        string(6) "ACTIVE"
      }

在这里我想获取的数据["displayName"]=>

使用get_object_vars()函数。

$data = get_object_vars($res); // data contain array value

你可以循环$data

foreach($data as $a){
   if(!is_array($a)) $a = get_object_vars($a);
   foreach($a as $b){
       //loop and cek while is object value
   }
}