Skip to content

Instantly share code, notes, and snippets.

@babyfaceEasy
Last active December 5, 2018 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save babyfaceEasy/431abad764b301430857ce8d380e414b to your computer and use it in GitHub Desktop.
Save babyfaceEasy/431abad764b301430857ce8d380e414b to your computer and use it in GitHub Desktop.
This is the response to Wilber Group PHP developer challenge
<?php
/**
* Instructions:
*
* Given the above JSON, build a simple PHP script to import it.
*
* Your script should create two variables:
*
* - a comma-separated list of email addresses
* - the original data, sorted by age descending, with a new field on each record
* called "name" which is the first and last name joined.
*
* Please deliver your code in either a GitHub Gist or some other sort of web-hosted code snippet platform.
*/
$people = '{"data":[{"first_name":"jake","last_name":"bennett","age":31,"email":"jake@bennett.com","secret":"VXNlIHRoaXMgc2VjcmV0IHBocmFzZSBzb21ld2hlcmUgaW4geW91ciBjb2RlJ3MgY29tbWVudHM="},{"first_name":"jordon","last_name":"brill","age":85,"email": "jordon@brill.com","secret":"YWxidXF1ZXJxdWUuIHNub3JrZWwu"},]}';
$people = rtrim($people, ',');
$last_comma = strrpos($people, ',');
//var_dump($last_comma);
$people = substr_replace($people, '', $last_comma, 1);
// json decode string into associative array.
$result = json_decode($people, true);
$email_addresses = '';
$sorted_arr = array();
foreach ($result['data'] as $data) {
$email_addresses .= $data['email'] . ', ';
$temp_arr = $data;
$temp_arr['name'] = $data['first_name'] . ' ' . $data['last_name'];
array_push($sorted_arr, $temp_arr);
}
uasort($sorted_arr, function($a, $b){
if ($a['age'] == $b['age']) {
return 0;
}
return ($a['age'] < $b['age']) ? -1 : 1 ;
});
// remove the last_comma
$email_addresses = substr_replace($email_addresses, '', strrpos(trim($email_addresses), ','), 1);
print_r($email_addresses);
echo "<br>";
print_r($sorted_arr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment