Skip to content

Instantly share code, notes, and snippets.

@toondaey
Created December 4, 2018 17:43
Show Gist options
  • Save toondaey/fc2143499f4857cc035a3deae14aae7a to your computer and use it in GitHub Desktop.
Save toondaey/fc2143499f4857cc035a3deae14aae7a to your computer and use it in GitHub Desktop.
Solution to coding assignment
<?php
include_once 'json.php';
$people = json_decode($people, true);
$data = $people['data'];
$email = '';
usort($data, 'compare');
function compare ($prev, $next) {
if ($prev['age'] == $next['age']) {
return 0;
}
return ($prev['age'] > $next['age']) ? -1 : 1;
}
$data = array_map(function ($obj) use (&$email) {
$email .= "{$obj['email']},";
$obj['name'] = "{$obj['first_name']} {$obj['last_name']}";
return $obj;
}, $data);
$email = trim($email, ',');
<?php
$people = '{"data":[{"first_name":"jake","last_name":"bennett","age":31,"email":"[email protected]","secret":"VXNlIHRoaXMgc2VjcmV0IHBocmFzZSBzb21ld2hlcmUgaW4geW91ciBjb2RlJ3MgY29tbWVudHM="},{"first_name":"jordon","last_name":"brill","age":85,"email": "[email protected]","secret":"YWxidXF1ZXJxdWUuIHNub3JrZWwu"}]}';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment