I have this Code to get JSON data from the API:
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'
);
$trends = $api->itemGet($params); // get data from api
$names = array();
foreach($trends as $trend) { // loop through the returned data
$names[] = $trend->lastvalue;
}
} catch(Exception $e) {
// Exception in ZabbixApi catched
echo $e->getMessage();
}
The response is:
"result": [
{
"itemid": "23298",
"hostid": "10084",
"lastvalue": "2552",
"name": "Disk root used p"
},
As you can see I made an array ($names
) with only the "lastvalue" in it. Now I'm trying to sort these values by the "hostid". Is this possible and how?
First you have to sort $trends
and then you can create the $names
You can do that by using the usort
function.
It takes an array and the Name of a Function you want to use to do the sorting.
For example
function sort_trends_by_hostid($a, $b) {
if ( $a->hostid == $b->hostid ) {
return 0;
}
return ($a->hostid < $b->hostid) ? -1 : 1;
}
usort($trends, 'sort_trends_by_hostid');
Source David Walsh