Last active
December 5, 2018 19:58
-
-
Save babyfaceEasy/431abad764b301430857ce8d380e414b to your computer and use it in GitHub Desktop.
This is the response to Wilber Group PHP developer challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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