Search code examples
phparraysjsonapizabbix

Zabbix Reading the Api


I'm getting Informations from the Zabbix Api with a PHP library. At the moment I get the "lastvalue" from the json Array:

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'            => 'extend', 
                            'sortfield'         => 'name',
                            'limit'             => '1'

                            );



    $trends = $api->itemGet($params); // get data from api

  $names = array();
    foreach($trends as $trend)  {       // loop through the returned data
      $names[] = $trend->lastvalue;

    }
//print_r($names);

} catch(Exception $e) {

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

But now I want to get the "lastvalue" plus the "name" of the Item in this Array for example like that: "name"+"lastvalue". How can I get both of them into my array $names[]?


Solution

  • Here is my answer from my comments, hope its what you are looking for!

    $trends = $api->itemGet($params);
    $names = array();
    foreach($trends as $trendKey => $trendValue)
    {
        $names[$trendKey] = $trendValue;
    }
    
    #Test the names array
    foreach ($names as $nameKey => $nameValue)
    {
        print("{$nameKey} = {$nameValue}<br />");
    }
    

    Return value:

    groupids = 2
    real_items = TRUE
    ...
    sortfield = name
    limit = 1