2015-11-03 39 views
1

我有以下代碼從ZabbixAPI閱讀項目名稱:的zabbix JSON得到陣列,而不JSON對象

try { 
    // connect to Zabbix-API 
    $api = new ZabbixApi($api_url, $username, $password); 

    $params = array(   'groupids'   => '2 ', 
          'real_items'  =>TRUE,      
          'monitored_items' =>TRUE, 
          'search' => array('name' => 'Disk root used p'),                  
          'selectFunctions' => 'extend', 
          'output'   => 'name', 
          'sortfield'   => 'name', 
          'lastvalue'   => 'value' 

               ); 

    $items = $api->itemGet($params);  // get data from api 
echo serialize($items); 


    foreach($items as $item) {  // loop through the returned data 

     echo "<td>".$item."</td>"; 



    } 

} catch(Exception $e) { 

    // Exception in ZabbixApi catched 
    echo $e->getMessage(); 
} 

有了,我得到這個輸出爲每個項目:

stdClass Object ([itemid] => 81351 [name] => Disk root used p) 

但我只需要該項目而不是JSON對象的「名」,所以輸出就像一個數組:itemname1, itemname2....

回答

1

你可以做到以下幾點:

$names = array(); 
foreach($items as $item) {  // loop through the returned data 
    $names[] = $item->name; 
} 

$ names數組將是項目名稱數組。

+0

謝謝你對我完美的工作。 :) – julien