Search code examples
phparraysjsonzabbix

Zabbix json get Array without json Object


I have following code to read Item names from the ZabbixAPI:

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();
}

With that I get this Output for every Item:

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

But I only need the 'name' of the Item and not the json objects so the output is just an array like that: itemname1, itemname2....


Solution

  • You can do the following:

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

    The $names array will be the array of item names.