2015-11-05 76 views
-1

嗨我想在這個對象中獲得名稱,我如何獲得每個stdClass的這些名稱。從stdClass對象獲取數據

stdClass Object 
(
    [page] => 0 
    [items] => 10 
    [programItems] => stdClass Object 
     (
      [programItem] => Array 
       (
        [0] => stdClass Object 
         (
          [@id] => 8955 
          [name] => name 
         ) 
        [1] => stdClass Object 
         (
          [@id] => 8955 
          [name] => name1 

我得到這個數據與

$data = json_decode(file_get_contents("http:/example.com")); 
+1

@Uchiha爲什麼要將完美的Object轉換爲數組。 _不計算_ – RiggsFolly

+0

是的你說得對@RiggsFolly我可能是錯的。 –

回答

1

這應該做的伎倆:

$programItem = $data->programItems; 

foreach($programItem as $item){ 
    echo $item->name; 
} 
+0

$ programItem = $ data-> programItems-> programItem; \t \t \t foreach($ programItem as $ item){ \t \t \t echo $ item-> name; \t}這個伎倆,謝謝 –

1

考慮$arr有STD對象數據,

$arr->page; 
$arr->items; 
foreach($arr->programItems as $a){ 
    foreach($a as $b) { 
     echo $b->name; 
     ... 
    } 
} 
0

這裏你的意志獲取json數據in $ stdobj

$stdobj=json_decode(file_get_contents("http:/example.com")); 
        echo "Page = ".$stdobj->page."<br>"; 
        echo "item = ".$stdobj->item."<br>"; 
        foreach ($stdobj->programItems as $datas) { 
         foreach ($datas as $data) { 
          foreach ($data as $key => $value) { 
           echo 'key = ' . $key . ', value :' . $value . '<br/>'; 
          } 
         } 
        }