I'm attempting to create a new mapping in an array using a calculation between to existing mappings.
Example:
'(space/total_space)' => [
'key' => 'Disk_Free_Percentage',
],
Where 'space' is divided by 'total_space' to get a percentage.
The code I am using this far is below and I am stuck on this last piece. Any help or pointers would be greatly appreciated.
// Call API to get servers
$rawData = $admin->servers();
// Data mapping
$map = [
'server_name' => [
'key' => 'Node',
],
'space' => [
'key' => 'Disk_Free_GB',
],
'total_space' => [
'key' => 'Total_Disk_GB',
],
'ram' => [
'key' => 'Free_RAM_GB',
'normalize' => function ($data) {
return number_format($data / 1024, 2);
},
],
'total_ram' => [
'key' => 'Total_RAM_GB',
'normalize' => function ($data) {
return number_format($data / 1024, 2);
},
],
'numvps' => [
'key' => 'VMs',
],
'version' => [
'key' => 'Version',
],
];
// Remap data
$data = [];
foreach ($rawData['servs'] as $row) {
$item = [];
foreach ($map as $fieldName => $field) {
// Validate key exists
if (!isset($field['key'])) {
throw new LogicException(sprintf('Field %s must supply a key', $fieldName));
}
// Get data from API
if (isset($row[$fieldName])) {
$value = $row[$fieldName];
} else {
// Use null if data not given by API
$value = null;
}
// Run normalize where set
if (isset($field['normalize']) && is_callable($field['normalize'])) {
$value = call_user_func($field['normalize'], $value);
}
// Map to new data
$item[$field['key']] = $value;
}
$data[] = $item;
}
// encode JSON
$json = json_encode($data)
Hi there
I can do this calculation for you. You can see my track record. I can finish this job in the next hour. Looking forward to hear from you.
Thanks
Rinsad
Hello!
With 98% to 99% completion rate, 850+ successfully completed projects, and a 4.99 reputation (maximum possible, 5.0) (can be verified on my profile page https://www.freelancer.com/u/rajeshsonisl.html !!)... you can never go wrong choosing me :)
I am available to get started on your project right away. I look forward to your reply.
Thanks.
Kind Regards,
Rajesh Soni
Dear sir, I will complete your project asap, LIFETIME error free guarantee and provide ONE YEAR support... I have right Skills required PHP, Software Architecture to execute your esteemed project.
Dear Sir
we are capable of performing the required task perfectly with accuracy,
please let us know if you want to clarify in details..
if you want us to share our skill and previous work
please initiate the chat by replying..
hope to hear you soon..
--
Thanks & Regards
Moumita