2010-10-14 139 views
1

正在爲我的問題尋找解決方案。 Seams和我一樣,但仍然沒有解決我的問題。stdClass對象foreach PHP

我有一個stdClass對象,需要打印出來的foreach或像這樣的somlike。

下面是我用「print_r($ result)」得到的結果的一個剪輯。

stdClass Object 
(
    [ServiceGroup] => Array 
     (
      [0] => stdClass Object 
       (
        [Service] => Array 
         (
          [0] => stdClass Object 
           (
            [_] => 3D-modulering 
            [count] => 71 
           ) 

          [1] => stdClass Object 
           (
            [_] => CAD 
            [count] => 81 
           ) 

          [2] => stdClass Object 
           (
            [_] => Databasutveckling 
            [count] => 118 
           ) 

          [3] => stdClass Object 
           (
            [_] => Datainmatare 
            [count] => 6 
           ) 

          [4] => stdClass Object 
           (
            [_] => Driftsteknik 
            [count] => 87 
           ) 

          [5] => stdClass Object 
           (
            [_] => IT-konsult 
            [count] => 39 
           ) 

          [6] => stdClass Object 
           (
            [_] => IT-tekniker 
            [count] => 223 
           ) 

          [7] => stdClass Object 
           (
            [_] => Mjuk- och hårdvarutestning 
            [count] => 150 
           ) 

          [8] => stdClass Object 
           (
            [_] => Nätverksteknik 
            [count] => 142 
           ) 

          [9] => stdClass Object 
           (
            [_] => Produkttestare 
            [count] => 171 
           ) 

          [10] => stdClass Object 
           (
            [_] => Programmerare 
            [count] => 146 
           ) 

          [11] => stdClass Object 
           (
            [_] => Projektledning 
            [count] => 156 
           ) 

          [12] => stdClass Object 
           (
            [_] => Serviceteknik 
            [count] => 157 
           ) 

          [13] => stdClass Object 
           (
            [_] => Support 
            [count] => 360 
           ) 

          [14] => stdClass Object 
           (
            [_] => Systemadministration 
            [count] => 145 
           ) 

          [15] => stdClass Object 
           (
            [_] => Systemutveckling 
            [count] => 110 
           ) 

          [16] => stdClass Object 
           (
            [_] => Webbdesign 
            [count] => 246 
           ) 

          [17] => stdClass Object 
           (
            [_] => Webbutveckling 
            [count] => 135 
           ) 

          [18] => stdClass Object 
           (
            [_] => webmaster 
            [count] => 103 
           ) 

         ) 

        [name] => Data/IT 
       ) 

      [1] => stdClass Object 
       (
        [Service] => Array 
         (
          [0] => stdClass Object 
           (
            [_] => Affärsanalys 
            [count] => 192 
           ) 

          [1] => stdClass Object 
           (
            [_] => Aktuarie 
            [count] => 20 
           ) 
+0

我沒有得到你想要的。更具體地說明你想要打印的內容。 – NikiC 2010-10-14 14:44:47

+0

stdClass是可迭代的,所以它可以在foreach中使用。你究竟想達到什麼目的? – 2010-10-14 14:45:37

回答

2

我不知道你想要什麼,但可以像數組一樣輕鬆地通過對象進行foreach。這個例子顯示。

foreach($result->ServiceGroup as $value){ 
    foreach($value->Service as $obj){ 
     echo $obj->_; 
     echo $obj->count; 
    } 
    echo $value->name; 
} 
+0

您是否知道要排序「服務」數組,需要首先顯示計數最高的「_」。 – 2010-10-14 15:05:10

+0

沒問題。如果我沒有咖啡因,我並不總是認真思考。 – 2010-10-14 15:05:29

+0

給我一下,我可以做一個排序。 – 2010-10-14 15:06:23