Find Jobs
Hire Freelancers

Calculated Field from Array

$10-15 USD

Closed
Posted over 8 years ago

$10-15 USD

Paid on delivery
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)
Project ID: 9047097

About the project

4 proposals
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
4 freelancers are bidding on average $42 USD for this job
User Avatar
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
$20 USD in 0 day
4.9 (1089 reviews)
8.9
8.9
User Avatar
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
$19 USD in 1 day
5.0 (955 reviews)
8.7
8.7
User Avatar
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.
$39 USD in 1 day
5.0 (113 reviews)
6.7
6.7
User Avatar
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
$89 USD in 1 day
4.8 (78 reviews)
6.3
6.3

About the client

Flag of UNITED STATES
United States
0.0
0
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.