Find Jobs
Hire Freelancers

Calculated Field from Array - Existing Code

$10-20 USD

Cancelled
Posted over 8 years ago

$10-20 USD

Paid on delivery
I'm attempting to create a new mapping for a calculated field of two existing mappings. Something like: '(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)
Project ID: 9046824

About the project

1 proposal
Remote project
Active 8 yrs ago

Looking to make some money?

Benefits of bidding on Freelancer

Set your budget and timeframe
Get paid for your work
Outline your proposal
It's free to sign up and bid on jobs
1 freelancer is bidding on average $20 USD for this job
User Avatar
I can start immediately once you assign it to me. I have seen your code. I am having experience in php.
$20 USD in 1 day
5.0 (1 review)
1.8
1.8

About the client

Flag of UNITED STATES
United States
0.0
0
Payment method verified
Member since Dec 3, 2015

Client Verification

Thanks! We’ve emailed you a link to claim your free credit.
Something went wrong while sending your email. Please try again.
Registered Users Total Jobs Posted
Freelancer ® is a registered Trademark of Freelancer Technology Pty Limited (ACN 142 189 759)
Copyright © 2024 Freelancer Technology Pty Limited (ACN 142 189 759)
Loading preview
Permission granted for Geolocation.
Your login session has expired and you have been logged out. Please log in again.